cURL POST command line on WINDOWS RESTful service
我的问题:
在与POST请求一起发送一些数据时,使用命令行工具卷曲本地主机服务器不起作用。
似乎引起错误的原因:
想象这样的事情
返回数据的结果
1 2 3 4 | 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版本使它正常工作:
1 | C:\Projects\curl-7.23.1-win64-ssl-sspi>curl -H"Content-Type: application/json" -X POST http://localhost/someapi -d"{"Name":"Test Value"}" |
它与第二个转义版本的区别仅在于,在转义符和标头参数值之间使用双引号。绝对更喜欢linux shell语法。
比起用引号引起来的命令行更容易使用的另一种替代方法是将json放入文件中,并使用curl参数的@前缀,例如在json.txt中包含以下内容:
1 2 3 4 5 | {"syncheader" : { "servertimesync" :"20131126121749", "deviceid" :"testDevice" } } |
然后以我为例:
1 | curl localhost:9000/sync -H"Content-type:application/json" -X POST -d @json.txt |
也使json更具可读性。
替代解决方案:比命令行更用户友好的解决方案:
If you are looking for a user friendly way to send and request data
using HTTP Methods other than simple GET's probably you are looking
for a chrome extention just like this one http://goo.gl/rVW22f called
AVANCED REST CLIENT
对于希望保留命令行的家伙,我推荐cygwin:
I ended up installing cygwin with CURL which allow us to Get that
Linux feeling - on Windows!Using Cygwin command line this issues have stopped and most important,
the request syntax used on 1. worked fine.
有用的链接:
Where i was downloading the curl for windows command line?
For more information on how to install and get curl working with
cygwin just go here
我希望它对某人有帮助,因为我整个上午都在此上度过。
至少对于我测试的Windows二进制版本(通用Win64 no-SSL二进制,当前基于7.33.0),您在解析命令行参数的方式上受到限制。 xmas的答案描述了该设置中的正确语法,该语法也可以在批处理文件中使用。使用提供的示例:
1 | curl -i -X POST -H"Content-Type: application/json" -d"{""data1"":""data goes here"",""data2"":""data2 goes here""}" http:localhost/path/to/api |
避免必须处理转义字符(取决于用于解析命令行的库)的更干净的选择是将标准json格式的文本存储在单独的文件中:
1 | curl -i -X POST -H"Content-Type: application/json" -d"@body.json" http:localhost/path/to/api |
要保留数据中的引号,请尝试像这样(")两次将其转义。
1 2 | curl ... -d"{""data1"":""data1 goes here"",""data2"":""data2 goes here""}" curl ... -d"{""data"":""data \""abc\"" goes here""}" |
Powershell上的另一种跨平台解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $headers = @{ 'Authorization' = 'Token 12d119ad48f9b70ed53846f9e3d051dc31afab27' } $body = @" { "value":"3.92.0", "product":"847" } "@ $params = @{ Uri = 'http://local.vcs:9999/api/v1/version/' Headers = $headers Method = 'POST' Body = $body ContentType = 'application/json' } Invoke-RestMethod @params |