我读过几篇关于这个主题的文章,但我不知道如何在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
型
我做错了什么?
4条答案
按热度按时间dxpyg8gm1#
所以,在进一步阅读和mohsen的回答之后,我最终确定了我的命令行:
字符串
我将JSON添加到一个名为exported.json的文件中,现在它更紧凑,并且仍然可以工作。
json数据本身不需要太多的调整,请参阅exported.json文件:
型
cgvd09ve2#
看起来像Windows命令行和PowerShell弄乱了curl和JSON post数据。在CMD,PowerShell和WSL中尝试了以下操作。WSL给出了我预期的结果,其他两个失败。(最初尝试将数据发布到Node-Red,他们的官方示例失败了,那是我的WTF时刻)
试试这个(httpbin.org只是在这种情况下回显你的数据):
第一个月
从WSL调用时的响应:
字符串
从CMD / PS呼叫时的响应:
型
注意
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(在
@`符号前打勾)smdncfj33#
首先缩小你的json here,不要用漂亮的json。
试试这个:
字符串
有关更多信息,请查看此site
gpnt7bae4#
我想我会分享我在这方面的发现,以防它帮助别人。
对我来说,为了让它工作,我只需要切换单引号和双引号,具有讽刺意味的是,这使得JSON格式不正确:
不工作:
字符串
有效:
型