cURL 命令在 git bash 中有效,但在 cmd 和 powershell 中无效 [英] cURL command works in git bash but not in cmd and powershell
问题描述
以下命令在 git bash 中有效,但在 cmd 和 powershell 中无效
The folowing command works in git bash but not in cmd and 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"
我在 cmd 中遇到错误,例如 -
I get error in cmd such as -
curl: (6) 无法解析主机:应用程序
curl: (6) Could not resolve host: application
curl: (6) 无法解析主机:Val 1,b
curl: (6) Could not resolve host: Val 1,b
curl: (6) 无法解析主机:Val 2,c
curl: (6) Could not resolve host: Val 2,c
curl: (6) 无法解析主机:Val 3,d
curl: (6) Could not resolve host: Val 3,d
curl: (3) [globbing] 第 6 列中无与伦比的大括号/括号
curl: (3) [globbing] unmatched close brace/bracket in column 6
可能是什么问题?
推荐答案
请阅读错误信息:
Invoke-WebRequest 无法绑定参数Headers".无法转换内容类型:application/json" 要键入的System.String"类型的值System.Collections.IDictionary".
Invoke-WebRequest Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type "System.String" to type "System.Collections.IDictionary".
在 PowerShell 中,curl
是 Invoke-WebRequest
cmdlet 的别名.正如错误指出的那样,Header
参数必须是 IDictionary,而不是字符串.这是它在 PowerShell 中的样子:
In PowerShell, curl
is an alias for the Invoke-WebRequest
cmdlet. As the error points it out, the Header
parameter must be a IDictionary, not a string. This is how it looks like in PowerShell:
@{"Content-Type"="application/json"}
有些参数也不同.这是我编写请求脚本的方式:
Some parameters are also different. This is how I would script the request:
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
这篇关于cURL 命令在 git bash 中有效,但在 cmd 和 powershell 中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!