简体   繁体   English

Powershell curl 双引号

[英]Powershell curl double quotes

I am trying to invoke a curl command in powershell and pass some JSON information.我正在尝试在 powershell 中调用 curl 命令并传递一些 JSON 信息。

Here is my command:这是我的命令:

curl -X POST -u username:password -H "Content-Type: application/json" -d "{ "fields": { "project": { "key": "key" }, "summary": "summary", "description": "description - here", "type": { "name": "Task" }}}"

I was getting globbing errors and "unmatched braces" and host could not be resolved, etc.我遇到了通配错误和“不匹配的大括号”,并且无法解析主机等。

Then I tried prefixing the double quotes in the string with the backtick character, but it could not recognize the - character in the description json field然后我尝试在字符串中的双引号前面加上反引号字符,但它无法识别描述 json 字段中的-字符

thanks谢谢

EDIT 1:编辑 1:

When I wrote the curl command in a regular batch file, I used double quotes and no single quotes.当我在常规批处理文件中编写 curl 命令时,我使用了双引号而不是单引号。 Also, in the -d string, I escaped all the double quotes with \\ and the command worked.此外,在-d字符串中,我用\\转义了所有双引号并且命令有效。

In this case, my curl is actually pointing to curl.exe.在这种情况下,我的curl实际上指向 curl.exe。 I specified the path, just didn't list it here.我指定了路径,只是没有在这里列出。 Also I tried adding single quotes around -d and I got:我也尝试在-d周围添加单引号,我得到:

curl: option -: is unknown curl: try 'curl --help' or 'curl --manual' for more information

Seems like it cannot recognize the - character in the JSON似乎无法识别 JSON 中的-字符

Pipe the data into curl.exe, instead of trying to escape it.将数据通过管道传输到 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 reads stdin if you use @- as your data parameter.如果您使用@-作为数据参数,curl.exe 会读取标准输入。

PS: I strongly suggest you use a proper data structure and ConvertTo-Json , as shown, instead of building the JSON string manually. PS:我强烈建议您使用正确的数据结构和ConvertTo-Json ,如图所示,而不是手动构建 JSON 字符串。

简单方法(用于简单测试):

curl -X POST -H "Content-Type: application/json" -d '{ \"field\": \"value\"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM