我的问题: 在发送一些数据和POST请求时,使用命令行工具卷曲本地主机服务器是不起作用的。
导致错误的原因: 想象一下这样的事情
curl -i -X POST -H 'Content-Type: application/json' -d '{"data1": "data goes here", "data2": "data2 goes here"}' http:localhost/path/to/api
返回数据的结果
curl: (6) Could not resolve host: application; No data record of requested type
curl: (6) Could not resolve host: data goes here,; No data record of requested type
curl: (6) Could not resolve host: data2; No data record of requested type
curl: (3) [globbing] unmatched close brace/bracket at pos 16
经过一些搜索,我发现问题不可能是请求使用的sintax,因为它在unixshell上工作。
Are you possibly using Windows? That so looks like a completely broken shell that doesn't properly deal with single-quotes vs double-quotes. I just tried that command line and it worked fine on my linux box. http://curl.haxx.se/mail/archive-2011-03/0066.html
我试着解决那些“逃避它”的问题,但仍然没有成功
2。
curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api
三。
curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api
所以我放弃了。 Windows似乎搞乱了POST上发送的JSON对象
我在我的win7 x64笔记本电脑上遇到了同样的问题,通过使用非常相似的命令行格式,我可以使用标记为Win64 - Generic w SSL的curl版本使它正常工作:
它只与第二个转义版本不同,在转义的和头参数值周围使用双引号。当然更喜欢linux shell语法。
命令行的另一个比使用引号更简单的选择是将json放入一个文件中,并使用curl参数的@前缀,例如json.txt中的以下内容:
在我的案例中,我发布:
使json更具可读性。
替代解决方案:比命令行更方便用户的解决方案:
对于想继续使用命令行的人,我推荐cygwin:
有用链接:
我希望它能帮助别人,因为我花了一上午的时间在这上面。
相关问题 更多 >
编程相关推荐