【问题标题】:Invoke-RESTMethod error "request_data must be of type: dict"调用 RESTMethod 错误“request_data 必须是类型:dict”
【发布时间】:2020-11-13 16:08:56
【问题描述】:

我刚开始使用 REST API,我唯一可用的语言是 PowerShell

我正在调用“事物”列表 - 这有效,我得到了 ID 属性

然后我尝试遍历该列表并获取“事物”(第二个 Invoke-RestMethod)的更详细记录 - 当我尝试这样做时,我得到一个错误:

Invoke-RestMethod : {"reply": {"err_code": 500, "err_msg": "处理 XDR 公共 API 时输入无效", "err_extra": "request_data 必须是类型:dict"}}

我认为 convertto-json 会将哈希表“请求”转换为字典。

我是新手,所以请不要以为我什么都知道。

$ContentType = "application/json"
$URL_All = 'https://api-<company>.xdr.eu.paloaltonetworks.com/public_api/v1/endpoints/get_endpoints/'
$URLEndpoint = 'https://api-<company>.xdr.eu.paloaltonetworks.com/public_api/v1/endpoints/get_endpoint/'

$endpoints = Invoke-RestMethod -uri $URL_All -method POST -Headers @{"x-xdr-auth-id" = "1"; "Authorization" = "<API-KEY_Goes_Here>" } -ContentType "application/json" -Verbose
if ($endpoints.reply.count -gt 0){
    write-host "Found $($endpoints.reply.count) details"
}
else {
    write-host " API didn't return any endpoint - ending script "
    break
}
foreach ($endpoint in $endpoints.reply) {
    if($endpoint.agent_id -ne $null){
    $request = @{
        request_data = '{}'
        filters      = @{
            field       = "endpoint_id_list"
            operator    = "in"
            Value       = "$($endpoint.agent_ID)"
            Search_from = 0
            Search_to   = 1
            sort        = @{ 
                field   = "last_seen"
                keyword = "desc"
            }
        } 
    } | ConvertTo-Json

    $Endpoint_full += Invoke-RestMethod -uri $URLEndpoint -method POST -Body $request -Headers @{"x-xdr-auth-id" = "1"; "Authorization" = "<API-KEY_Goes_Here>" } -ContentType "application/json" -Verbose
    }
}

【问题讨论】:

  • @iRon 总是值得一看,在这种情况下,它使用默认深度 2 转换为 Json 并返回没有问题。

标签: powershell rest


【解决方案1】:

您的 JSON 转换不是问题,提示在错误消息中。

"request_data must be of type: dict"

密钥是request_data,它只包含在您的请求中:

request_data = '{}'

即。它基本上是空白的,而 API 需要一个哈希表或类似的结构。

【讨论】:

  • 谢谢你,我确实把 request_data 的内容放错了地方。下面产生名为“Filters”的哈希表的行应该在 request_data 表中。它已经解决了这个错误并传递了另一个......显然过滤器需要是“列表”类型......我已将您的回复标记为答案,因为它解决了原始查询。
猜你喜欢
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 2022-01-27
  • 2019-08-16
  • 2023-03-24
  • 2018-03-26
相关资源
最近更新 更多