无法在Windows中使用curl POST JSON数据

z9smfwbn  于 4个月前  发布在  Windows
关注(0)|答案(4)|浏览(81)

我读过几篇关于这个主题的文章,但我不知道如何在windows 10(powershell)上使用curl POST发送json数据。
我试过使用“”或“nothing. json data:

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

字符串
curl尝试示例:

> 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


与“

> 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: (3) [globbing] unmatched brace in column 1
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
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1


我做错了什么?

dxpyg8gm

dxpyg8gm1#

所以,在进一步阅读和mohsen的回答之后,我最终确定了我的命令行:

curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d @exported.json

字符串
我将JSON添加到一个名为exported.json的文件中,现在它更紧凑,并且仍然可以工作。
json数据本身不需要太多的调整,请参阅exported.json文件:

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

cgvd09ve

cgvd09ve2#

看起来像Windows命令行和PowerShell弄乱了curl和JSON post数据。在CMD,PowerShell和WSL中尝试了以下操作。WSL给出了我预期的结果,其他两个失败。(最初尝试将数据发布到Node-Red,他们的官方示例失败了,那是我的WTF时刻)
试试这个(httpbin.org只是在这种情况下回显你的数据):
第一个月
从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 "Content-type:application/json" https://httpbin.org/anything
这在PowerShell中有效,但在其他两个curl -d '{\"name\":\"Nick\",\"a\":32}' -H "Content-type:application/json" https://httpbin.org/anything中失败
当然,powershell有它自己的规则,所以在其他人中你可以做-d @data.json,但在powershell中你必须做-d@data.json(在@`符号前打勾)

smdncfj3

smdncfj33#

首先缩小你的json here,不要用漂亮的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

字符串
有关更多信息,请查看此site

gpnt7bae

gpnt7bae4#

我想我会分享我在这方面的发现,以防它帮助别人。
对我来说,为了让它工作,我只需要切换单引号和双引号,具有讽刺意味的是,这使得JSON格式不正确:

不工作:

curl -X POST -H 'Content-type: application/json' -d '{"text":"Hello World"}' https://my-unique-slack-hooks-url-here.com

字符串

有效:

curl -X POST -H 'Content-type: application/json' -d "{'text':'Hello, World'}" https://my-unique-slack-hooks-url-here.com

相关问题