简体   繁体   English

Powershell:使用 Invoke-WebRequest,如何在 JSON 中使用变量?

[英]Powershell: Using Invoke-WebRequest, How To Use Variable Within JSON?

Using Powershell, I'm trying to PUT data into an API, but I'm having trouble using a variable within the JSON:使用 Powershell,我正在尝试将数据放入 API,但在使用 JSON 中的变量时遇到问题:

This code below does not generate an error from the API, but it PUTS the singer as, literally, "$var_currentsinger" and doesn't use the intended variable下面的代码不会从 API 生成错误,但它将singer作为字面意思是“$var_currentsinger”并且不使用预期的变量

$currentsinger = "Michael Jackson"
$headers.Add("Content-Type", "application/json")
$response = Invoke-WebRequest -Uri https://stackoverflow.com -Method PUT -Headers $headers -Body '{
  "album": {
    "name": "Moonlight Sonata",
    "custom_fields": [{
      "singer": "$currentsinger",
      "songwriter": "Etta James"
    }]
  }
}'

This version below does not work, I assume because there are no quotes around the singer name.下面的这个版本不起作用,我假设是因为singer名字周围没有引号。 The API returns a value that the data is invalid API 返回数据无效的值

$currentsinger = "Michael Jackson"
$headers.Add("Content-Type", "application/json")
$response = Invoke-WebRequest -Uri https://stackoverflow.com -Method PUT -Headers $headers -Body '{
  "album": {
    "name": "Moonlight Sonata",
    "custom_fields": [{
      "singer": $currentsinger,
      "songwriter": "Etta James"
    }]
  }
}'

The only thing I have tried is double quotes and triple quotes around the variable, but I either get the $currentsinger variable within the JSON and have it submit the variable value and not the variable name.我唯一尝试过的是变量周围的双引号和三引号,但我要么在 JSON 中获取 $currentsinger 变量,然后让它提交变量值而不是变量名。

JSON requires double-quotes, so one example way to handle is by escaping the quotes or with a double-quoted here-string: JSON 需要双引号,因此处理的一种示例方法是转义引号或使用双引号 here-string:

# here-string
$json = @"
  "singer": $currentsinger,
  "songwriter": "Etta James"
"@

# escaped
$json = "
  `"singer`": $currentsinger,
  `"songwriter`": `"Etta James`"
"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 PowerShell 中的 Invoke-WebRequest 解析 JSON? - How to parse JSON from the Invoke-WebRequest in PowerShell? 如何解析/访问 Powershell 4 中 Invoke-WebRequest 返回的 JSON - How to parse/access JSON returned by Invoke-WebRequest in Powershell 4 我正在尝试使用invoke-restmethod / invoke-webrequest在powershell中执行REST API,但在传递Json输入时失败 - I am trying to execute REST API within powershell using invoke-restmethod/invoke-webrequest but failed when I pass Json inputs 将 cURL 转换为 PowerShell Invoke-WebRequest - JSON 数据表问题 - Convert cURL to PowerShell Invoke-WebRequest - JSON data table issue Powershell Invoke-Webrequest w/ JSON Body - 无法反序列化……? - Powershell Invoke-Webrequest w/ JSON Body - Can not deserialize…? 如何为具有多个来自CSV变量的API调用设计“ Invoke-WebRequest”? (电源外壳) - How to design an “Invoke-WebRequest” for multiple API calls with more than one variable from CSV? (Powershell) 如何从调用Invoke-WebRequest访问JSON? - How do I access the JSON from calling Invoke-WebRequest? Powershell Invoke-WebRequest XML 数据 - Powershell Invoke-WebRequest XML data Powershell Invoke-WebRequest和字符编码 - Powershell Invoke-WebRequest and character encoding 如何使用Invoke-WebRequest的GET方法来构建查询字符串 - How to use GET method of Invoke-WebRequest to build a query string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM