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:
- The “json string” with the
application/x-www-form-urlencoded
header is not considered valid data, and the API discards it. - The “key value pair” with the
application/x-www-form-urlencoded
header is considered valid data, and the API accepts it.
The default header forInvoke-WebRequest
isapplication/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-RestMethod
and-ContentType "application/json"
.
Comments
0 comments
Article is closed for comments.