WINDOWS RESTful servi上的cURL POST命令行

2024-04-19 09:57:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题: 在发送一些数据和POST请求时,使用命令行工具卷曲本地主机服务器是不起作用的。

导致错误的原因: 想象一下这样的事情

  1. 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对象


Tags: topathjsonlocalhosthttpdatahereapplication
3条回答

我在我的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\"}"

它只与第二个转义版本不同,在转义的和头参数值周围使用双引号。当然更喜欢linux shell语法。

命令行的另一个比使用引号更简单的选择是将json放入一个文件中,并使用curl参数的@前缀,例如json.txt中的以下内容:

{ "syncheader" : {
    "servertimesync" : "20131126121749",
    "deviceid" : "testDevice"
  }
}

在我的案例中,我发布:

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

我希望它能帮助别人,因为我花了一上午的时间在这上面。

相关问题 更多 >