cURL 命令在 git bash 中有效,但在 cmd 和 powershell 中无效

Rag*_*pta 6 api powershell curl cmd git-bash

以下命令适用于 git bash 但不适用于 cmd 和 powershell

curl -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"
Run Code Online (Sandbox Code Playgroud)

我在 cmd 中收到错误,例如 -

curl: (6) 无法解析主机:应用程序

curl: (6) 无法解析主机:Val 1,b

curl: (6) 无法解析主机:Val 2,c

curl: (6) 无法解析主机:Val 3,d

curl: (3) [globbing] 第 6 列中无与伦比的大括号/括号

可能是什么问题?

Mar*_*ndl 1

只需阅读错误消息:

Invoke-WebRequest 无法绑定参数“标头”。无法将“System.String”类型的“Content-Type: application/json”值转换为“System.Collections.IDictionary”类型。

在 PowerShell 中,curl是 cmdlet 的别名Invoke-WebRequest。正如错误指出的那样,Header参数必须是 IDictionary,而不是字符串。这是它在 PowerShell 中的样子:

@{"Content-Type"= "application/json"}

有些参数也不同。这就是我编写请求脚本的方式:

Invoke-WebRequest `
    -Uri "http://localhost:5678/api/findgen" `
    -Headers @{"Content-Type"= "application/json"} `
    -Body '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' `
    -OutFile "file.json" `
    -Method Post
Run Code Online (Sandbox Code Playgroud)

  • 该答案所指的错误消息在问题中不存在。该问题显示来自真实卷曲的错误消息。这非常令人困惑! (3认同)