Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在windows中使用curl发布json数据_Json_Curl - Fatal编程技术网

Can';在windows中使用curl发布json数据

Can';在windows中使用curl发布json数据,json,curl,Json,Curl,我读过几篇关于这个主题的文章,但我不知道如何在Windows10(powershell)上用curl-POST发送json数据 我没有用\“或”“”试过。 json数据: { "frames": [ { "text": "HOME2", "icon": "i294", "index": 0 }, { "text": "? 65",

我读过几篇关于这个主题的文章,但我不知道如何在Windows10(powershell)上用curl-POST发送json数据

我没有用\“或”“”试过。 json数据:

{
    "frames": [
        {
            "text": "HOME2",
            "icon": "i294",
            "index": 0
        },
        {
            "text": "? 65",
            "icon": null,
            "index": 1
        }
    ]
}
卷曲尝试的示例:

> curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d "{ """frames""": [{ """text""": """HOME2""", """icon""": """i294""", """index""": 0 }, { """text""": """? 65""", """icon""": null, """index""": 1 }]}"
{"error":{"code":null,"message":"Bad Request","trace":["request body must not be empty"]}}curl: (3) [globbing] bad range specification in column 2
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: HOME2,
curl: (6) Could not resolve host: icon
curl: (6) Could not resolve host: i294,
curl: (6) Could not resolve host: index
curl: (6) Could not resolve host: 0
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: ? 65,
curl: (6) Could not resolve host: icon
curl: (6) Could not resolve host: null,
curl: (6) Could not resolve host: index
curl: (6) Could not resolve host: 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
用\“

我做错了什么

首先缩小json,不要使用漂亮的json

试试这个:

curl -d 'curl -d '{"frames":[{"text":"HOME2","icon":"i294","index":0}, 
 {"text":"? 65","icon":null,"index":1}]}' -H 'Content-Type: application/json' 
 https://example.com/login

有关更多信息,请查看此

,因此,在进一步阅读并从mohsen的回答中,我将命令行定稿为:

curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d @exported.json
我将json添加到一个名为export.json的文件中,现在它更紧凑了,并且仍然可以工作

json数据本身不需要太多调整,请参见exported.json文件:

{"frames":[{"text":"HOME2","icon":"i294","index":"0"},{"text":"? 65","icon":"null","index":"1"}]}

看起来Windows命令行和PowerShell会处理curl和JSON post数据。在CMD、PowerShell和WSL中尝试了以下操作。WSL给出了我预期的结果,其他两个失败。(最初尝试将数据发布到Node Red,但他们的官方示例失败,这是我的WTF时刻)

试试这个(httpbin.org只是在本案例中回显您的数据):

curl-xpost-d'{“name”:“Nick”,“a”:32}'-H“内容类型:application/json”https://httpbin.org/anything

从WSL调用时的响应:

{
  "args": {},
  "data": "{\"name\":\"Nick\",\"a\":32}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "22",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.71.1"
  },
  "json": {
    "a": 32,
    "name": "Nick"
  },
  "method": "POST",
  "origin": "*********",
  "url": "https://httpbin.org/anything"
}
从CMD/PS调用时的响应:

{
  "args": {},
  "data": "'{name:Nick,a:32}'",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "18",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.55.1"
  },
  "json": null,
  "method": "POST",
  "origin": "****",
  "url": "https://httpbin.org/anything"
}
注意
Content Length
不同,从cmd或ps调用时,响应中的
json
属性为
null

但是这样做在WSL和cmd中都可以,但在PowerShell中不行:
curl-d“{\'name\:\'Nick\”,\“a\”:32}“-H”内容类型:application/jsonhttps://httpbin.org/anything

这在PowerShell中工作,但在另外两个
curl-d'{\'name\':\'Nick\',\'a\':32}'-H“内容类型:application/json”中失败https://httpbin.org/anything


当然,powershell有它自己的规则,所以在其他程序中,你可以执行
-d@data.json
,但在powershell中,你必须输入
-d`@data.json
(在
@
符号前打勾)

这有帮助,但仅适用于cygwin windows。在windows powershell上,它仍然会引发错误。您的示例是的,但如果我使用我的json数据集,它仍然会在windows powershell中抱怨。我用最新发现更新了我的问题,以及现在有效的方法。不要更新问题,只需回答您的问题以改善社区即可
{
  "args": {},
  "data": "'{name:Nick,a:32}'",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "18",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.55.1"
  },
  "json": null,
  "method": "POST",
  "origin": "****",
  "url": "https://httpbin.org/anything"
}