【问题标题】:Powershell Invoke-RestMethod POST has issues with jsonPowershell Invoke-RestMethod POST 与 json 有问题
【发布时间】:2020-06-03 19:50:45
【问题描述】:

我正在尝试使用 REST API 在 RecoverPoint for Virtual Machines (RP4VM) 中配置一些警报。我正在尝试使用 json 同时输入多个过滤器。 json 文件如下所示:

[
{
  "JsonSubType": "SystemEventLogsFilter",
  "level": "WARNING",
  "scope": "NORMAL",
  "eventsIDs": [],
  "filterUID": {
    "id": 1570417688566256135
  },
  "name": "RPA_issue",
  "topic": "RPA",
  "groupsToInclude": null
},
{
  "JsonSubType": "SystemEventLogsFilter",
  "level": "WARNING",
  "scope": "ADVANCED",
  "eventsIDs": [],
  "filterUID": {
    "id": -1728986321682574312
  },
  "name": "cluster_events",
  "topic": "CLUSTER",
  "groupsToInclude": null
}
]

当我尝试运行脚本时出现错误:

Unexpected token (START_ARRAY), expected START_OBJECT: need JSON Object to contain As.PROPERTY type information (for class com.emc.fapi.version5_2.commons.SystemEventLogsFilter)
 at [Source: org.apache.catalina.connector.CoyoteInputStream@75b592c2; line: 1, column: 1]

如果我删除方括号,它会执行第一个值而不是第二个值。这是我的代码问题还是他们的问题?

脚本:

$rp4vmcl = import-csv -Path .\test_clusters.csv
$credential = Get-Credential
$username = $credential.GetNetworkCredential().UserName
$password = $credential.GetNetworkCredential().password
$credPair = "$($username):$($password)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
$headers = @{ 
    Authorization = "Basic $encodedCredentials"; 
    "Accept" = "application/json";
    "Content-Type" = "application/json"
}
$comp = "/system/event_logs_filters"
$json = Get-Content .\event_log_filter.json -Raw
foreach ($s in $rp4vmcl) {
    $cluster = $s.cluster_name
    $uid = $s.cluster_uid
    $curl = $s.cluster_url
    $url = "$curl$comp"
    $cluster
    $results = Invoke-RestMethod -Method POST -uri $url -SkipCertificateCheck -Headers $headers -Body $json
}

【问题讨论】:

  • 脚本是什么?

标签: powershell


【解决方案1】:

如果接收者希望每个 Json 对象调用一次,它将无法处理数组。它完全依赖于 web 服务的实现。顺便说一句,最好换个身体:

$results = Invoke-RestMethod -Method POST -uri $url -SkipCertificateCheck -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($json))

先试试这个,如果没有帮助,循环遍历元素并调用网络服务个体:

    $jsonObject = $json | ConvertFrom-Json
    $results = @()
    $jsonObject | foreach {
    $json = $_  | ConvertTo-Json -Depth 99
    $results += Invoke-RestMethod -Method POST -uri $url -SkipCertificateCheck -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($json))
    }

顺便说一句,powershell

"application/json"

而不是

"application/json; charset=utf-8"

powershell 会将其误解为 Windows 编码。因此,如果您遇到编码问题,您有四种选择:

1.) 如果可以修改webservice,修改响应头

2.) 切换到更新的 powershell 版本(推荐 v7)

3.) 直接通过 .net cmdlet 构建您自己的 Web 服务调用

4.) 使用invoke-webservice,将答案直接写入带有OutFile 参数的文件中

另见: Powershell Invoke-RestMethod incorrect character

【讨论】:

  • 运行 PS 7。我遇到的唯一问题是无法进行多次插入。我感觉这是供应商代码。
  • 如果 web 服务不能处理多个插入,这是供应商设计的,您必须单独进行调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 2015-02-04
  • 1970-01-01
  • 2017-01-12
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多