json - cURL到PowerShell-哈希表

标签 json powershell curl hashtable

因此,在将我先前的cURL发布到PowerShell哈希表问题(已解决)之后,我现在遇到了另一个问题,这次是使用data(?)中的3个哈希表将cURL转换为PowerShell(或更可能是我的PowerShell技能) )。这次,我的脚本通过PowerShell返回:

...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脚本,它是使用我在前面的问题中给出的建议构建的:

cURL to PowerShell - Double hash table in --data?
$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\"
                                      }
             }
}

返回此错误:

{"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'字段中的正斜杠?我尝试了第二次-replace,但仍然遇到相同的错误:
(($Body | ConvertTo-Json) -replace '"', '\"' -replace '/', '\/')
(($Body | ConvertTo-Json) -replace '"', '\"' -replace '/', '\2f')
(($Body | ConvertTo-Json) -replace '"', '\"' -replace '/', '\%2f')

数据哈希表中可能缺少方括号吗?它们在cURL中,但在PowerShell中不存在,但是在我以前的脚本中,我没有方括号,而且PowerShell似乎不喜欢它们。

可以通过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
    

    关于json - cURL到PowerShell-哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53463771/

    相关文章:

    javascript - 检查图表值输入的字符串值的多个 JSON 属性

    string - Internet Explorer 截断包含 JSON 的 flashVar

    powershell - 在 Azure 中创建 VM 快照

    PowerShell 分析变量和对象

    javascript - (node.js) 如何从远程服务器获取cookie?

    json - ConvertFrom-Json 返回 "Cannot process argument because the value of argument "名称“无效”。

    java - GSON。如何将json对象转换为json数组?

    powershell - $_ 在 PowerShell 中意味着什么?

    ruby - 如何使用 -X GET -G 选项将 curl 转换为 Ruby Net::HTTP?

    docker - Nginx proxy_reverse用于docker容器