Powershell卷曲双引号

Kin*_*ere 5 powershell json curl escaping character-encoding

我试图在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" }}}"
Run Code Online (Sandbox Code Playgroud)

我得到了错误和"无与伦比的大括号",主机无法解决,等等.

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

谢谢

编辑1:

当我在常规批处理文件中编写curl命令时,我使用双引号而没有单引号.此外,在-d字符串中,我转义所有双引号\和命令工作.

在这种情况下,我curl实际上是指向curl.exe.我指定了路径,只是没有在这里列出.我还尝试添加单引号-d,我得到:

curl: option -: is unknown curl: try 'curl --help' or 'curl --manual' for more information
Run Code Online (Sandbox Code Playgroud)

好像它无法识别-JSON中的字符

Tom*_*lak 8

将数据传输到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 "@-"
Run Code Online (Sandbox Code Playgroud)

如果您@-用作数据参数,curl.exe将读取stdin .

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


Ale*_*eev 5

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

curl -X POST -H "Content-Type: application/json" -d '{ \"field\": \"value\"}'
Run Code Online (Sandbox Code Playgroud)