我目前在通过API发送燃料交易的脚本上遇到问题.它不需要变量就能工作:

$headers=@{}
$headers.Add("accept", "application/json")
$headers.Add("content-type", "application/json")
$headers.Add("authorization", "$APIToken")
$response = Invoke-WebRequest -Uri 'https://api.samsara.com/fuel-purchase' -Method POST -Headers $headers -ContentType 'application/json' -Body '{"fuelQuantityLiters":"676.8","iftaFuelType":"Diesel","transactionLocation":"350 Rhode Island St, San Francisco, CA 94103","transactionPrice":{"amount":"640.2","currency":"usd"},"transactionReference":"5454534","transactionTime":"2024-02-22T10:20:50.52-06:00","vehicleId":"570"}'

但是,对于变量,我会得到错误.

# A POSITIONAL PARAMETER CANNOT BE FOUND THAT ACCEPTS ARGUMENT ("" Around Body)
$response = Invoke-WebRequest -Uri 'https://api.samsara.com/fuel-purchase' -Method POST -Headers $headers -ContentType 'application/json' -Body "{"fuelQuantityLiters":$QuantityLiters,"iftaFuelType":$FuelType,"transactionLocation":$StationAddress,"transactionPrice":{"amount":$TransactionSale,"currency":$Currency},"transactionReference":$TransactionID,"transactionTime":$TransactionDateTime,"vehicleId":$Vehicle}"

# INVALID CHARACTER '$' LOOKING FOR BEGINNING OF VALUE ('' Around Body)
$response = Invoke-WebRequest -Uri 'https://api.samsara.com/fuel-purchase' -Method POST -Headers $headers -ContentType 'application/json' -Body '{"fuelQuantityLiters":$QuantityLiters,"iftaFuelType":$FuelType,"transactionLocation":$StationAddress,"transactionPrice":{"amount":$TransactionSale,"currency":$Currency},"transactionReference":$TransactionID,"transactionTime":$TransactionDateTime,"vehicleId":$Vehicle}'

我认为这与报价有关,但我不确定需要在哪里修正.以下是包含有关我正在执行的操作的API详细信息的页面: https://developers.samsara.com/reference/postfuelpurchase

推荐答案

我想这和引号有关.

事实上,你唯一迫在眉睫的问题是你在你的expandable (interpolating), double-quoted string ("...")-use 102 (or 103)岁中试图达到的you neglected to escape the 101 characters人:

# Note the use of `" inside the overall "..."
Invoke-WebRequest ... -Body "{`"fuelQuantityLiters`": ... }" 
  • 当您忽略对Embedded "进行转义时,您所指的单个字符串无意中变成了two个参数,这触发了您看到的错误.[1]

由于您需要扩展(字符串内插),因此可以 Select 使用verbatim (single-quoted string ('...')作为-Body参数的整体封闭形式.

  • 当您try 这样做时,try 的变量引用(例如$QuantityLiters)被简单地发送到您的Web服务,但失败了.

然而,正如其他人所指出的,最好将输入数据定义为hashtable(@{ ... }),并让ConvertTo-Json为您创建JSON表示:

Invoke-WebRequest ... -Body (
   @{ fuelQuantityLiters = $QuantityLiters; iftaFuelType = "Diesel" <# ; ... #> } |
   ConvertTo-Json
)

[1]原因是PowerShell解析compound个字符串参数的方式有个问题,您意外使用了这些参数:"{"foo": "bar"}"之类的标记由五个相邻的字符串组成:"{"foo": "bar"}").PowerShell不是像Bash那样简单地将它们拆分成two个参数{foo: bar},仅仅是因为first个子字符串是quoted("{");因为后面引用的子字符串foo,isn't与所有剩余的子字符串is连接在一起.句法引号(")如往常一样在该过程中被移除.您可以通过Write-Output "{"foo": "bar"}"验证此行为.有关详细信息,请参阅this answer.

Json相关问答推荐

try 将文本标记放置在条形图上的同一高度

Allof Indide的JSON模式之一

重构JOLT代码以获得预期输出

如何在VB6中将字符串转换或解码为可读格式?

简单条形图和有序分类中条形图的 colored颜色 梯度

如何避免解析 ISuperObject 类型字段中的 json 对象

爆炸没有数组的 struct pyspark

如何使用Powershell查找所有包含特定键值对的JSON对象并迭代所有对象?

在Databricks中如何将JSON文件作为字典读取

通过 xslt 将内部 json 转换为 xml 时遇到问题

如果值不存在,则将值插入 JSON 数组

使用带有逗号的字段名称构建 struct

未捕获的类型错误:无法读取 null 的属性props

Rails:format.js 或 format.json,或两者兼而有之?

如何从 mysql 数据库构建 JSON 数组

在 Rails 中使用 JSON 创建嵌套对象

将字符串映射到json对象的多种类型?

如何将 mysqli 结果转换为 JSON?

带有 Jackson 的不可变 Lombok 注释类

你如何在 Arrays of Arrays 上 OPENJSON