我正在尝试在 powershell 中调用 curl 命令并传递一些 JSON 信息。
这是我的命令:
curl -X POST -u username:password -H "Content-Type: application/json" -d "{ "fields": { "project": { "key": "key" }, "summary": "summary", "description": "description - here", "type": { "name": "Task" }}}"
我遇到了通配错误和“不匹配的大括号”,并且无法解析主机等。
然后我尝试用反引号字符在字符串中的双引号前面加上前缀,但它无法识别
-
描述 json 字段中的字符谢谢
编辑 1:
当我在常规批处理文件中编写 curl 命令时,我使用了双引号而不是单引号。此外,在
-d
字符串,我用 \
转义了所有的双引号并且命令有效。在这种情况下,我的
curl
实际上是指向 curl.exe。我指定了路径,只是没有在这里列出。我也尝试在 -d
周围添加单引号我得到了:curl: option -: is unknown curl: try 'curl --help' or 'curl --manual' for more information
好像无法识别
-
JSON 中的字符
最佳答案
将数据通过管道传输到 curl.exe,而不是尝试对其进行转义。
$data = @{
fields = @{
project = @{
key = "key"
}
summary = "summary"
description = "description - here"
type = @{
name = "Task"
}
}
}
$data | ConvertTo-Json -Compress | curl.exe -X POST -u username:password -H "Content-Type: application/json" -d "@-"
如果您使用
@-
,curl.exe 会读取 stdin作为您的数据参数。P.S.:我强烈建议你使用合适的数据结构和
ConvertTo-Json
,如图所示,而不是手动构建 JSON 字符串。
关于json - Powershell curl 双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33526823/