Does Automox API Accept Non-JSON Payloads?

Does Automox API Accept Non-JSON Payloads?

The Automox API does not accept non-JSON payloads. There was a case where it appeared that it did using an API script such as:

$apiKey = 'access_key_here' # API key used is associated with my user account, which is a Global Administrator
$headers = @{ "Authorization" = "Bearer $apiKey" }

Write-Output("remediateServer:")

$url = "https://console.automox.com/api/policies/policy_id/action?o=org_id&serverId=server_id"
$body = @{
"action" = "remediateServer"

}


try
{
Invoke-WebRequest -Method POST -Uri $url -Headers $headers -Body $body
}
catch
{
$response = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($response)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$response = $reader.ReadToEnd();
$responseCode = $_.Exception.Response.StatusCode.value__
Write-Output "Response: ${response}"
Write-Output "Response Code: ${responseCode}"
}

This API method does indeed work, but please note:

  1. The “json string” with theapplication/x-www-form-urlencodedheader is not considered valid data, and the API discards it.
  2. The “key value pair” with theapplication/x-www-form-urlencodedheader is considered valid data, and the API accepts it. 

The default header forInvoke-WebRequestisapplication/x-www-form-urlencoded. The important thing to note is that the content-type header is telling the API that the JSON string is not JSON and that it's not valid.  It's not so much that we accept non-JSON payloads; the key pairs get converted when a request comes in with that header. 

This would be the correct iteration:

$apiKey = 'access_key_here' # API key used is associated with my user account, which is a Global Administrator
$headers = @{ "Authorization" = "Bearer $apiKey" }

Write-Output("remediateAll:")

$url = "https://console.automox.com/api/policies/policy_id/action?o=org_id"
$body = @"
{
"action": "remediateAll"

}
"@



try
{
Invoke-RestMethod -Method POST -ContentType "application/json" -Uri $url -Headers $headers -Body $body
}
catch
{
$response = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($response)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$response = $reader.ReadToEnd();
$responseCode = $_.Exception.Response.StatusCode.value__
Write-Output "Response: ${response}"
Write-Output "Response Code: ${responseCode}"
}

For clarity, this was added: Invoke-RestMethodand-ContentType "application/json"

Was this article helpful?
0 out of 0 found this helpful