由于Windows curl.exe native program 解释单引号和双引号的方式,我只能在 Windows 10(内部版本 17063 或更高版本)上看到带有 C:\Windows\System32\curl.exe 的“Could not resolve host”。
如果你反转/转义这些引号,它确实有效(在 CMD 上)
C:\Windows\System32\curl -X POST http://localhost:5678/api/findgen \
-H "Content-Type: application/json" \
^^^
double-quotes
-d "{\"a\": \"Val 1\",\"b\": \"Val 2\",\"c\": \"Val 3\",\"d\": \"Val 4\"}" -o "file.json"
^^^
double quotes outside, escaped double-quotes inside: valid JSON
这类似于“How do I POST JSON with Curl?”
在 Powershell 上,我必须转义双引号(反引号,如评论),并转义 {}: {{}}
PS D:\> C:\Windows\System32\curl -X POST https://reqbin.com/echo/post/json `
-H "Content-Type: application/json" `
-d "{{`"login`":`"my_login`",`"password`":`"my_password`"}}"
或者:
PS D:\> C:\Windows\System32\curl.exe -X POST https://reqbin.com/echo/post/json `
>> -H "Content-Type: application/json" `
>> -d '{{""login"":""my_login"",""password"":""my_password""}}'
{"success":"true"}
来自史蒂文的帖子:
PS D:\> C:\Windows\System32\curl.exe -X POST -H "Content-Type: application/json" `
-d '{""north"": ""each south""}' https://reqbin.com/echo/post/json
{"success":"true"}
单独使用 curl.exe 意味着使用 MingW Git for Windows curl.exe
PS D:\> curl.exe -X POST -H "Content-Type: application/json" `
>> -d '{""north"": ""each south""}' https://reqbin.com/echo/post/json
curl: (6) Could not resolve host: each
curl: (3) unmatched close brace/bracket in URL position 6:
south} https://reqbin.com/echo/post/json
因此,您需要转义大括号,并删除所有空格:
PS D:\> curl.exe -X POST -H "Content-Type: application/json" `
>> -d '{{"north":"each south"}}' https://reqbin.com/echo/post/json
{"success":"true"}
或者,使用空格,单引号:
PS D:\> curl.exe -X POST -H "Content-Type: application/json" `
>> -d '{\"north\": \"each south\"}' https://reqbin.com/echo/post/json
{"success":"true"}
在 CMD 上如果您想保持这些引号不变,请使用与 Git For Windows 打包的 mingw curl.exe,至少在 CMD 上。
C:\path\to\git\mingw64\bin\curl.exe -X POST http://localhost:5678/api/findgen \
-H 'Content-Type: application/json' \
-d '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' -o "file.json"
^^^
You can keep the quotes as-is
作为commented,在Powershell 会话中,curl 本身就是Invoke-WebRequest 的别名。
我测试过:
PS D:\> Get-Alias -Name curl
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
PS D:\> curl -uri https://reqbin.com/echo/post/json `
>> -Method 'POST' -ContentType "application/json" `
>> -Body "{""login"":""my_login"",""password"":""my_password""}"
StatusCode : 200
StatusDescription : OK
Content : {"success":"true"}
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
CF-Cache-Status: DYNAMIC
cf-request-id: 0917a97460000053c82aa69000000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/bea...
Forms : {}
Headers : {[Connection, keep-alive], [CF-Cache-Status, DYNAMIC], [cf-request-id, 0917a97460000053c82aa69000000001], [Expect-CT, max-age=604800,
report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 19