【发布时间】:2015-11-04 16:18:36
【问题描述】:
我正在尝试在 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" }}}"
我遇到了 globbing 错误和“不匹配的大括号”以及无法解析主机等问题。
然后我尝试在字符串中的双引号前加上反引号字符,但它无法识别描述json字段中的-字符
谢谢
编辑 1:
当我在常规批处理文件中编写 curl 命令时,我使用了双引号而没有使用单引号。此外,在-d 字符串中,我使用\ 转义了所有双引号,并且该命令有效。
在这种情况下,我的curl 实际上指向 curl.exe。我指定了路径,只是没有在这里列出。我还尝试在-d 周围添加单引号,我得到了:
curl: option -: is unknown curl: try 'curl --help' or 'curl --manual' for more information
似乎无法识别 JSON 中的 - 字符
【问题讨论】:
-
1) 尝试将
-d参数用单引号而不是双引号括起来 2) 确保实际上正在调用curl.exe。如果我没记错的话,powershell 中的curl可以是Invoke-WebRequest的别名 -
你好,请检查上面的编辑
-
好的,那可能是转义了。在 powershell 中,转义字符是反引号 ` 所以可以尝试在引号 inside json 上使用它
-
@StevenPenny 在我看来,您想清理所有 curl/powershell 线程 ^^。像
curl.exe -X POST "https://reqbin.com/echo/post/json" -H "Content-Type: application/json" -d (@{fields=@{project=@{key='key';summary='summary';description='description - here'; type=@{name='Task'}}}} | ConvertTo-Json -Depth 3 -Compress)这样的东西会适用吗?
标签: json powershell curl character-encoding escaping