Objective
To clarify payload formatting requirements for the Automox API and explain how to correctly structure PowerShell API calls using application/json.
Overview
The Automox API requires data payloads to be formatted as valid JSON strings accompanied by an explicit Content-Type: application/json HTTP header.
While certain non-JSON payloads (such as key-value pairs submitted via application/x-www-form-urlencoded) may appear to succeed under specific conditions, the Automox API automatically discards malformed or misclassified data payloads. To ensure consistent API execution, all state-changing API requests (POST, PUT, PATCH) must supply structured JSON strings.
Technical Cause
When making API calls via PowerShell:
Invoke-WebRequestvs.Invoke-RestMethod: By default, PowerShellPOSTrequests send aContent-Typeheader set toapplication/x-www-form-urlencodedunless explicitly overridden.Header Mismatch: If a raw JSON string is submitted under a
form-urlencodedheader, the API parser cannot interpret the data structure correctly and discards the payload.Expected Behavior: Explicitly setting
-ContentType "application/json"informs the API gateway to parse the request body as structured JSON.
Correct Implementation (PowerShell Example)
Use Invoke-RestMethod along with an explicit application/json Content-Type header to ensure the API receives and processes the payload correctly.
PowerShell
# Automox API Request Example: Triggering Policy Remediation
# 1. Define API Credentials and Headers
$apiKey = 'YOUR_API_KEY'
$headers = @{
"Authorization" = "Bearer $apiKey"
}
# 2. Define Endpoint Parameters
$orgID = 'YOUR_ORG_ID'
$policyID = 'YOUR_POLICY_ID'
$url = "https://console.automox.com/api/policies/$policyID/action?o=$orgID"
# 3. Construct JSON Payload Body
$body = @"
{
"action": "remediateAll"
}
"@
# 4. Execute API Request with Explicit JSON Content-Type
try {
$response = Invoke-RestMethod -Method POST `
-Uri $url `
-Headers $headers `
-ContentType "application/json" `
-Body $body
Write-Output "Policy action executed successfully."
$response
}
catch {
# Extract detailed response content from error stream
if ($_.Exception.Response) {
$stream = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$errorBody = $reader.ReadToEnd()
$statusCode = [int]$_.Exception.Response.StatusCode
Write-Error "API Request Failed - Status Code $statusCode : $errorBody"
} else {
Write-Error "Execution error: $_"
}
}
Key Takeaways & Best Practices
| Parameter / Command | Incorrect Approach | Correct Approach |
| PowerShell Cmdlet | Invoke-WebRequest (Requires manual string conversion) | Invoke-RestMethod (Native REST/JSON handling) |
| Content-Type Header | Omitted / application/x-www-form-urlencoded | -ContentType "application/json" (Mandatory) |
| Payload Structure | PowerShell Hashtable without conversion | Formatted JSON string or hashtable piped through ConvertTo-Json |