简体   繁体   English

如何在 powershell 字符串中转义“引号”?

[英]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.

相关问题 如何将我的序列化JSON字符串包装在“单引号”中 - How do I wrap my serialized JSON string in 'single quotes' 如何在 Microsoft Graph 中转义 Sharepoint 列名称? - How do I escape a Sharepoint column name in Microsoft Graph? 我如何将curl转换为powershell - How do i convert curl to powershell Powershell,转义正则表达式转义方法中不存在的字符串变量中的特定字符 - Powershell, escaping specific character in string variable not present in regex escape method 如何在PowerShell中使用REST API在TFS中创建错误? - How do I create a bug in TFS using REST API in PowerShell? 如何将JSON提交到PowerShell RestMethod或WebRequest中 - How do I submit JSON into a PowerShell RestMethod or WebRequest 如何让 ORDS 查询返回不带引号的 JSON_OBJECT? - How do I make an ORDS query return a JSON_OBJECT without quotes? 如何阻止 DevOps REST API 将传入的 JSON 有效负载包装在双引号中? - How do I stop the DevOps REST API from wrapping an incoming JSON payload in double quotes? Java Spring Request正文中的转义引号 - Escape quotes in java spring request body 如何使用Json.Net从我的WCF休息服务(.NET 4)返回json,而不是字符串,用引号括起来? - How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM