我的问题: 使用命令行工具来 curl 我的本地主机服务器,同时发送一些数据和我的 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
经过一番搜索后,我发现问题不可能是用于请求的语法,因为它适用于 UNIX shell。
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
3.
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 版本让它工作。通过使用非常相似的命令行格式:
C:\Projects\curl-7.23.1-win64-ssl-sspi>curl -H "Content-Type: application/json" -X POST http://localhost/someapi -d "{\"Name\":\"Test Value\"}"
它与您的第二个转义版本的唯一区别在于在转义版本和 header 参数值周围使用双引号。绝对更喜欢 linux shell 语法。
关于windows - WINDOWS RESTful 服务上的 cURL POST 命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11834238/