sailingbikeruk
sailingbikeruk

Reputation: 326

Invoke-RESTMethod error "request_data must be of type: dict"

I am just starting out with REST API and the only language I have available to me is PowerShell

I am calling a list of "things" - this works and I get the ID property

I then try to iterate through that list and get a more detailed record for the "thing" (The second Invoke-RestMethod) - when I attempt this I get an error:

Invoke-RestMethod : {"reply": {"err_code": 500, "err_msg": "Got an invalid input while processing XDR public API", "err_extra": "request_data must be of type: dict"}}

I thought that convertto-json would convert the hashtable "request" to a dictionary.

I am new to this so please don't assume I know anything.

$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
    }
}

Upvotes: 0

Views: 594

Answers (1)

PMental
PMental

Reputation: 1179

Your JSON conversion isn't the issue, the hint is in the error message.

"request_data must be of type: dict"

Key being request_data which just consists of this in your request:

request_data = '{}'

Ie. it's basically blank while the API expects a hash table or similar structure.

Upvotes: 1

Related Questions