Powershell:将json字符串传递给curl [英] Powershell: passing json string to curl
问题描述
我正在尝试使用curl将Powershell脚本中的JSON字符串传递给build.phonegap.com api.根据 phonegap的论坛,当在Windows计算机上运行时,必须对JSON数据进行格式化为:
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
确实,当从命令行调用时,这确实运行良好.
但是,当我尝试从Powershell脚本中调用它时,双引号似乎被剥夺了.
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:
- 将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
知道如何实现吗?
Any idea how to achieve this?
感谢您的宝贵时间,科恩
Thanks for your time, Koen
推荐答案
尝试使用-%
运算符将PowerShell置于简单的(哑)参数解析模式:
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
这通常对于调用带有PowerShell实参语法的实参语法的exe很有用.这确实需要PowerShell V3或更高版本.
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屋!