Powershell curl双引号 [英] Powershell curl double quotes

查看:159
本文介绍了Powershell curl双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在powershell中调用curl命令并传递一些JSON信息。

I am trying to invoke a curl command in powershell and pass some JSON information.

这是我的命令:

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.

然后我尝试在字符串中的双引号前加上反引号字符,但是它无法识别描述json字段中的-字符

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

谢谢

编辑1:

当我在常规批处理文件中编写curl命令时,我使用了双引号而不是单引号。另外,在 -d 字符串中,我使用 \ 转义了所有双引号,并且该命令有效。

When I wrote the curl command in a regular batch file, I used double quotes and no single quotes. Also, in the -d string, I escaped all the double quotes with \ and the command worked.

在这种情况下,我的 curl 实际上指向curl.exe。我指定了路径,只是未在此处列出。我也尝试在 -d 附近添加单引号,然后得到:

In this case, my curl is actually pointing to curl.exe. I specified the path, just didn't list it here. Also I tried adding single quotes around -d and I got:

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

似乎无法识别JSON中的-字符

Seems like it cannot recognize the - character in the JSON

推荐答案

将数据放入curl.exe,而不是尝试对其进行转义。

Pipe the data into curl.exe, instead of trying to escape it.

$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。

curl.exe reads stdin if you use @- as your data parameter.

PS:我强烈建议您使用适当的数据结构和 ConvertTo-Json ,如图所示,而不是手动构建JSON字符串。

P.S.: I strongly suggest you use a proper data structure and ConvertTo-Json, as shown, instead of building the JSON string manually.

这篇关于Powershell curl双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆