Windows 下的 curl 命令语法

3

我有以下命令,在Linux上可以正常工作,但在Windows上却不能。我找不到任何关于curl语法在Windows上的文档。我尝试了双引号...但还是无效。有人能帮我调整这个命令吗,以便我可以在Windows上使用它 (我已经安装了c盘中的curl.exe)。

curl -X POST -H "Content-Type: application/json" -H "X-Cachet-Token: secret" http://somegoodserver/api/v1/incidents -d '{"name":"Test","message":"Test message","status":"1"}'

我得到的错误是:
"status":400,"title":"Bad Request","detail":"The request cannot be fulfilled due to bad syntax.","meta": {"details":["The name format is invalid.","The status format is invalid.","The message format is invalid."]}}]}

注意:我正在Windows命令提示符中运行此命令。 - user1164061
1
尝试使用CreateProcessW WinAPI函数。 cmd.exe无法在参数中传递{},因此您的-d参数将变得损坏。 - hanshenrik
1个回答

13

在Windows中,每行末尾需要加上^,单引号必须替换为双引号(json括号内除外),在json数据内部,所有双引号都必须转义。

以下是在Win10和WinXP命令提示符上工作的GET和POST示例:

curl -X GET ^
-H "authorization: Basic [your authentication string, if needed]" ^
-H "cache-control: no-cache" ^
-H "content-type: application/json" ^
-H "x-forte-auth-organization-id: org_333251" ^
"https://sandbox.forte.net/api/v3/organizations/org_333251/locations/loc_191620/customers/"


curl -X POST ^
-H "authorization: Basic [your authentication string, if needed]" ^
-H "cache-control: no-cache" ^
-H "content-type: application/json" ^
-H "x-forte-auth-organization-id: org_333251" ^
-d "{ \"action\": \"sale\", \"authorization_amount\": 16.99, \"billing_address\": { \"first_name\": \"Paul\", \"last_name\": \"Hurst\" }, \"card\": { \"card_type\": \"visa\", \"name_on_card\": \"Forte James\", \"account_number\": \"4111111111111111\", \"expire_month\": \"12\", \"expire_year\": \"2020\", \"card_verification_value\": \"123\" } }" ^
"https://sandbox.forte.net/api/v3/organizations/org_333251/locations/loc_191620/transactions" ^

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接