cURL 到 PowerShell - 哈希 Table

cURL to PowerShell - Hash Table

因此,在将我之前的 cURL 发布到 PowerShell 哈希 Table 问题(已解决)之后,我现在 运行 进入了另一个问题,这次使用 3 哈希将 cURL 转换为 PowerShell tables in data(?)(或者更可能是我的 PowerShell 技能)。这一次,我的脚本通过 PowerShell returns:

...General Error java.util.LinkedHashMap cannot be cast to java.util.List

这是通过 Postman 完美运行的 cURL:

curl -X PATCH \
  https://example.com/apis/v1.1/parameters \
  -H 'Authorization: Bearer 1234567890' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
    "data": [
        {
            "DUID": 3299,
            "AID": 551,
            "CID": 10002,
            "Parameters": [
                {
                    "name": "Customer.1/AddressLine1",
                    "value": "SOMEWHERE ROAD"
                }
            ]
        }
    ]
}'

这是 PowerShell 脚本,我使用我之前在此处提出的问题中给出的建议构建的:

$CURLEXE = 'C:\Windows\System32\curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"

$Body =   @{
      'data'= @{
      'DUID'= 3299;
      'AID'= 551;
      'CID'= 10002;
      'Parameters'=
      @{'name'= "Customer.1/AddressLine1";
        'value'= "SOMEWHERE ROAD"}
                }
            }

$CurlArgument = '-X', 'PATCH',
                $URL1,
                '-H', 
                $AuthBearer,
                '-H', 'Content-Type: application/json',
                '-H', 'cache-control: no-cache',
                '-d', 
                (($Body | ConvertTo-Json) -replace '"', '\"')

& $CURLEXE @CurlArgument

我的 $CurlArgument 看起来像这样:

-X
PATCH
https://example.com/apis/v1.1/parameters
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-H
cache-control: no-cache
-d
{
    \"data\":  {
                 \"CID\":  10002,
                 \"DUID\":  3299,
                 \"AID\":  551,
                 \"Parameters\":  {
                                          \"value\":  \"SOMEWHERE ROAD\",
                                          \"name\":  \"Customer.1/AddressLine1\"
                                      }
             }
}

其中returns这个错误:

{"status":"FAILURE","errors":[{"code":"5004","name":"General Error","severity":"3","message":"Error Occured During the operation","details":{"5004":"General Error java.util.LinkedHashMap cannot be cast to java.util.List"}}]}

难道是 'Customer.1/AddressLine1' 字段中的正斜杠?我尝试用这些进行第二次替换,但仍然出现相同的错误:

(($Body | ConvertTo-Json) -replace '"', '\"' -replace '/', '\/')
(($Body | ConvertTo-Json) -replace '"', '\"' -replace '/', 'f')
(($Body | ConvertTo-Json) -replace '"', '\"' -replace '/', '\%2f')

会不会是data hash中缺少方括号table?它们存在于 cURL 中,但不存在于 PowerShell 中,但是在我之前的脚本中我没有方括号,而且 PowerShell 似乎不喜欢它们。

难道是"value"和"name"的顺序被PowerShell改变了?

如有任何帮助,我们将不胜感激。

更新 感谢 @mklement0 的帮助,我将我的 PowerShell 编辑为以下内容并解决了它!

$Body =   @{
      data = , @{
      DUID = 3299
      AID = 551
      CID = 10002
      Parameters = , @{
         name = "Customer.1/AddressLine1"
         value = "SOMEWHERE ROAD"
                             }
                }
            }

$CurlArgument = '-X', 'PATCH',
                'https://example.com/apis/v1.1/parameters',
                '-H', 
                $AuthBearer,
                '-H', 'Content-Type: application/json',
                '-H', 'cache-control: no-cache',
                '-d', 
                (($Body | ConvertTo-Json -Depth 4) -replace '"', '\"')

& $CURLEXE @CurlArgument

Lee Daily提供了关键指针:

您的JSON格式需要一个数组的子对象[...])作为值datadata.Parameters 属性,而您在 $Body 中构建的看似等效的哈希表只有一个标量子对象。

需要对您的代码进行两项调整:

  • 确保datadata.Parameters包含数组:

    • , <object>构造一个包含<object>的单元素数组; ConvertTo-Json 自动将数组转换为 JSON [ ... ] 表示法。
  • -Depth 4ConvertTo-Json结合使用,以确保对象层次结构中的所有子对象都完整表示。

$CURLEXE = 'C:\Windows\System32\curl.exe'
$URL1 = "https://example.com/apis/v1.1/parameters"

$Body = @{
  data = , @{ # Note the "," to construct an *array*
    DUID = 3299
    AID = 551
    CID = 10002
    Parameters = , @{ # Note the "," to construct an *array*
       name = "Customer.1/AddressLine1"
       value = "SOMEWHERE ROAD"
    }
  }
}

# Note the -Depth 4 in the ConvertTo-Json call.
$CurlArgument = '-X', 'PATCH',
                $URL1,
                '-H', 
                $AuthBearer,
                '-H', 'Content-Type: application/json',
                '-H', 'cache-control: no-cache',
                '-d', 
                (($Body | ConvertTo-Json -Depth 4) -replace '"', '\"')


& $CURLEXE @CurlArgument