WINDOWS RESTful 服务上的 cURL POST 命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11834238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:46:15  来源:igfitidea点击:

cURL POST command line on WINDOWS RESTful service

windowsapipostcurlcommand-line

提问by Lothre1

My problem:Using the command line tool to curl my localhost server while sending some data along with my POST request is not working.

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

What seems to be causing the error:Imagine something like this

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

  1. curl -i -X POST -H 'Content-Type: application/json' -d '{"data1": "data goes here", "data2": "data2 goes here"}' http:localhost/path/to/api
  1. curl -i -X POST -H 'Content-Type: application/json' -d '{"data1": "data goes here", "data2": "data2 goes here"}' http:localhost/path/to/api

Result of the returning data

返回数据结果

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

After some searching i figured out that problem couldn't be the sintax used for the request since it works on UNIX shells.

经过一番搜索,我发现问题不可能是用于请求的语法,因为它适用于 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

你可能使用 Windows 吗?这看起来像是一个完全损坏的外壳,无法正确处理单引号与双引号。我刚刚尝试了该命令行,它在我的 linux 机器上运行良好。 http://curl.haxx.se/mail/archive-2011-03/0066.html

I tried to work around with those " escaping it \" but it still didn't work

我试图解决那些“逃避它”的问题,但它仍然没有用

2.

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 go here\", \"data2\": \"data2 go here\"}' http://localhost/path/to/api

3.

3.

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 go here\", \"data2\": \"data2 go here\"}' http://localhost/path/to/api

So i gave up. Windows seems to messing up with the JSON object sent on POST

所以我放弃了。Windows 似乎搞乱了 POST 上发送的 JSON 对象

回答by user1683523

I ran into the same issue on my win7 x64 laptop and was able to get it working using the curl release that is labeled Win64 - Generic w SSLby using the very similar command line format:

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

Which only differs from your 2nd escape version by using double-quotes around the escaped ones and the header parameter value. Definitely prefer the linux shell syntax more.

这与您的第二个转义版本仅在转义和标头参数值周围使用双引号不同。绝对更喜欢 linux shell 语法。

回答by wwkudu

Another Alternative for the command line that is easier than fighting with quotation marks is to put the json into a file, and use the @ prefix of curl parameters, e.g. with the following in json.txt:

命令行的另一种比使用引号更容易的替代方法是将 json 放入文件中,并使用 curl 参数的 @ 前缀,例如在 json.txt 中使用以下内容:

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

then in my case I issue:

然后在我的情况下,我发出:

curl localhost:9000/sync -H "Content-type:application/json" -X POST -d @json.txt

Keeps the json more readable too.

也使 json 更具可读性。

回答by Lothre1

Alternative solution: A More Userfriendly solution than command line:

替代解决方案:比命令行更用户友好的解决方案:

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/rVW22fcalled AVANCED REST CLIENT

如果您正在寻找一种用户友好的方式来使用 HTTP 方法而不是简单的 GET 方法发送和请求数据,那么您可能正在寻找一种像http://goo.gl/rVW22f称为 AVANCED REST CLIENT的 chrome 扩展

For guys looking to stay with command-line i recommend cygwin:

对于希望使用命令行的人,我推荐 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.

我最终安装了带有 CURL 的 cygwin,这让我们能够在 Windows 上获得那种 Linux 的感觉!

使用 Cygwin 命令行此问题已停止,最重要的是,1. 上使用的请求语法工作正常。

Useful links:

有用的链接:

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 命令行的 curl?

有关如何安装和使用 cygwin 使用 curl 的更多信息,请访问此处

I hope it helps someone because i spent all morning on this.

我希望它可以帮助某人,因为我整个上午都在做这个。

回答by mysteryegg

At least for the Windows binary version I tested, (the Generic Win64 no-SSL binary, currently based on 7.33.0), you are subject to limitations in how the command line arguments are being parsed. The answer by xmas describes the correct syntax in that setting, which also works in a batch file. Using the example provided:

至少对于我测试的 Windows 二进制版本(通用 Win64 no-SSL 二进制文件,目前基于7.33.0),您会受到命令行参数解析方式的限制。xmas 的答案描述了该设置中的正确语法,这也适用于批处理文件。使用提供的示例:

curl -i -X POST -H "Content-Type: application/json" -d "{""data1"":""data goes here"",""data2"":""data2 goes here""}" http:localhost/path/to/api

A cleaner alternative to avoid having to deal with escaped characters, which is dependent upon whatever library is used to parse the command line, is to have your standard json format text in a separate file:

避免处理转义字符(依赖于用于解析命令行的任何库)的一种更简洁的替代方法是将标准 json 格式文本放在单独的文件中:

curl -i -X POST -H "Content-Type: application/json" -d "@body.json" http:localhost/path/to/api

回答by xmas

  1. Try to use double quotes (") instead of single ones (').
  2. To preserve JSON format quotes, try doubling them ("").
  3. To preserve quotes inside data, try to double-escape them like this (\\"").

    curl ... -d "{""data1"": ""data1 goes here"", ""data2"": ""data2 goes here""}"
    curl ... -d "{""data"": ""data \""abc\"" goes here""}"
    
  1. 尝试使用双引号 (") 而不是单引号 (')。
  2. 要保留 JSON 格式的引号,请尝试将它们加倍 ("")。
  3. 要保留数据中的引号,请尝试像这样 (\\"") 对它们进行双重转义。

    curl ... -d "{""data1"": ""data1 goes here"", ""data2"": ""data2 goes here""}"
    curl ... -d "{""data"": ""data \""abc\"" goes here""}"
    

回答by gek

One more alternative cross-platform solution on powershell 6.2.3:

powershell 6.2.3 上的另一种替代跨平台解决方案:

$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