[英]How do I escape “Quotes” in a powershell string?
I need to send a simple string to a web api using Powershell, and I have to ensure the value in the HTML body has "Quotes" around it, because the content type has to be application/json.我需要使用 Powershell 向 web api 发送一个简单的字符串,并且我必须确保 HTML 正文中的值周围有“引号”,因为内容类型必须是 application/json。
For example:例如:
$specialKey = "101010"
Invoke-RestMethod -Method Put `
-Uri $keyAssignmentUri `
-Header @{"content-type"="application/json"} `
-Body "$specialKey"
When I inspect this call in Fiddler, I see that the body is 101010 instead of "101010".当我在 Fiddler 中检查这个调用时,我看到正文是 101010 而不是“101010”。 How do I send the body value with quotes?
如何使用引号发送正文值?
In order to have the "Quotes", you need to provide the escape character (`) before every " which you want to print or send.为了拥有“引号”,您需要在要打印或发送的每个“之前提供转义字符 (`)。
$PrintQuotesAsString = "`"How do I escape `"Quotes`" in a powershell string?`""
Write-Host $PrintQuotesAsString
"How do I escape "Quotes" in a powershell string?"
用单引号将其括起来以使其成为文字字符串:
$specialKey = '"101010"'
Replacing double quotes (") with a sequence of back-slash, backtick and double-quotes worked for me:用一系列反斜杠、反引号和双引号替换双引号 (") 对我有用:
\`"
Example : I want to set (using the env command) an environment variable named SCOPES to the following value, then launch node app.js :示例:我想(使用env命令)将名为SCOPES的环境变量设置为以下值,然后启动node app.js :
{
"email":"Grant permissions to read your email address",
"address":"Grant permissions to read your address information",
"phone":"Grant permissions to read your mobile phone number"
}
so I used:所以我用:
env SCOPES="{ \`"email\`": \`"Grant permissions to read your email address\`", \`"address\`": \`"Grant permissions to read your address information\`", \`"phone\`": \`"Grant permissions to read your mobile phone number\`" }" node app.js
References: http://www.rlmueller.net/PowerShellEscape.htm参考资料: http : //www.rlmueller.net/PowerShellEscape.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.