Powershell:将 json 字符串传递给 curl [英] Powershell: passing json string to curl

查看:115
本文介绍了Powershell:将 json 字符串传递给 curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 curl 将 JSON 字符串从 powershell 脚本中传递到 build.phonegap.com api.
根据 phonegap 的论坛,在 Windows 机器上运行时,JSON 数据必须是格式为:

curl.exe -ku user@email:mypass -X PUT -d "data={"password":"keypass"}";https://build.phonegap.com/api/v1/key


事实上,当从命令行调用时,这确实运行良好.
但是,当我尝试从 powershell 脚本中调用它时,双引号似乎被剥离了.

到目前为止,我已经尝试过:

  • 将 JSON 放入单引号字符串中:

curl.exe -ku user@email:mypass -X PUT -d '"data={"password":"keypass"}"' https://build.phonegap.com/api/v1/key

  • 将 JSON 放入单引号字符串中,没有 DOS 转义反斜杠:

curl.exe -ku user@email:mypass -X PUT -d '"data={"password":"keypass"}"' https://build.phonegap.com/api/v1/key
  • 将 JSON 放入单引号字符串中,转义双引号和反斜杠(带有反斜杠的 DOS 样式):

curl.exe -ku user@email:mypass -X PUT -d '"data={\"password\":\"keypass\"}"' https://build.phonegap.com/api/v1/key

  • 将 JSON 放入双引号字符串中,使用 powershell 反引号字符 (`) 转义双引号:

curl.exe -ku user@email:mypass -X PUT -d "`"data={`"password`":`"build*2014``"}`"" https://build.phonegap.com/api/v1/key

知道如何实现这一目标吗?

感谢您的时间,科恩

解决方案

尝试使用 --% 运算符将 PowerShell 置于简单(哑)参数解析模式:

curl.exe --% -ku user@email:mypass -X PUT -d "data={"password":"keypass"}";https://build.phonegap.com/api/v1/key

这对于调用具有与 PowerShell 的参数语法相冲突的参数语法的 exe 非常有用.这确实需要 PowerShell V3 或更高版本.

I'm trying to pass a JSON string from within a powershell script to the build.phonegap.com api, using curl.
According to phonegap's forum, when running on a Windows machine, the JSON data has to be formatted as:

curl.exe -ku user@email:mypass -X PUT -d "data={"password":"keypass"}" https://build.phonegap.com/api/v1/key


Indeed, this does run fine when invoked from the command line.
However, when I try to invoke this from within a powershell script, the double quotes seem to be stripped.

So far, I have tried:

  • Putting the JSON in single quoted string:

curl.exe -ku user@email:mypass -X PUT -d '"data={"password":"keypass"}"'  https://build.phonegap.com/api/v1/key

  • Putting the JSON in single quoted string, without the DOS escape backslashes:

curl.exe -ku user@email:mypass -X PUT -d '"data={"password":"keypass"}"'  https://build.phonegap.com/api/v1/key

  • Putting the JSON in single quoted string, escaping the double quotes and backslashes (DOS style with a backslash):

curl.exe -ku user@email:mypass -X PUT -d '"data={\"password\":\"keypass\"}"'  https://build.phonegap.com/api/v1/key

  • Putting the JSON in a double quoted string, escaping the double quotes with the powershell backtick character (`):

curl.exe -ku user@email:mypass -X PUT -d "`"data={`"password`":`"build*2014``"}`""  https://build.phonegap.com/api/v1/key

Any idea how to achieve this?

Thanks for your time, Koen

解决方案

Try using the --% operator to put PowerShell into simple (dumb) argument parsing mode:

curl.exe --% -ku user@email:mypass -X PUT -d "data={"password":"keypass"}" https://build.phonegap.com/api/v1/key

This is quite often useful for invoking exes with argument syntax that runs afoul of PowerShell's argument syntax. This does require PowerShell V3 or higher.

这篇关于Powershell:将 json 字符串传递给 curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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