cURL 命令适用于 linux 但不适用于 windows

时间:2021-01-28 21:49:34

标签: linux windows curl

大家好, 我这里有一个 cURL 命令

curl -X POST https://*.com/graph/api/v1/graphql -H 'content-type:application/json' -H 'Authorization:bearer *token*' -d '{"query": "{ assets(query: { searchTerm: \"hello-world-sys-app-v1\" ,type: \"app\"}) { groupId, assetId, version, type } }"}'

这在 linux 上按预期运行并像这样返回

{"data":{"assets":[{"groupId":"*","assetId":"hello-world-sys-app-v1","version":"1.0.15-D160-B167-PERF","type":"app"},{"groupId":"*","assetId":"hello-world-sys-app-v1","version":"1.0.15-D157-B115-DEV1","type":"app"}]}}

但是在 Windows 上运行时出现这样的错误

The request's Content-Type is not supported. Expected:
application/jsoncurl: (6) Could not resolve host: application
curl: (6) Could not resolve host: bearer
curl: (6) Could not resolve host: dc411cee-c007-4955-ab45-87522b2713ad'
curl: (3) [globbing] nested brace in column 17

我尝试了一些解决方案,但没有运气 https://superuser.com/questions/1291352/curl-command-runs-in-linux-but-not-windows-2008

我尝试了几种不同的单引号和双引号组合,但都出现错误,我也尝试使用数据文件,但出现类似错误。我感觉这是一个简单的问题,但我似乎无法弄清楚语法。

任何帮助或想法将不胜感激!

谢谢

3 个答案:

答案 0 :(得分:0)

几天前我也遭受了同样的痛苦,在 Windows 上,-d 必须在双引号之间,因此您必须将它们转义到里面。像这样的东西(我没有测试过):

-d "{\"query\": \"{ assets(query: { searchTerm: \"hello-world-sys-app-v1\" ,type: \"app\"}) { groupId, assetId , 版本, 输入 } }\"}"

在 Windows 中,您在 PowerShell 中有 Invoke-RestMethod,这似乎(并且仅似乎)更准确。更多信息请访问:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.1

答案 1 :(得分:0)

如果您在 Windows 上使用 Git Bash 之类的东西,您可能会遇到正斜杠翻译的问题。

https://github.com/bmatzelle/gow/issues/196

答案 2 :(得分:0)

我现在可以在 Windows 上使用它,虽然我仍然不知道它的确切原因,但我首先用双引号替换了所有单引号,但仍然出现错误。但在添加三个反斜杠后,我能够得到正确的响应。

curl -X POST "https://*.com/graph/api/v1/graphql" -H "content-type:application/json" -H "Authorization:bearer *" -d "{\"query\": \"{ assets(query: { searchTerm: \\\"hello-world-sys-app-v1\\\" ,type: \\\"app\\\"}) { groupId, assetId, version, type } }\"}"