Getting the error "Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type" when trying to run powershell file in MID server
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2020 12:18 AM
Hi,
I am trying to monitor a Windows service from the MID server and trying to run a powershell file that creates an incident in servicenow when the service fails. I have used the default code available in Servicenow REST API explorer but I am getting the error "Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type" when I try to execute it by powershell command line.
The code I'm using is
# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "admin" # Build auth header $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass))) # Set proper headers $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo)) $headers.Add('Accept','application/json') # Specify endpoint uri $uri = "https://proximusdcdev.service-now.com/api/now/table/incident" # Specify HTTP method $method = "post" # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri # Print response $response.RawContent
I have replaced the user with the appropriate local user and u;dated the instance URL. Any idea what else needs to be done? The same functionality works using a curl coe from the MID server.
Regards,
AKshatha

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2020 01:57 AM
HI,
What is that
undefined
in the URL?
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2020 02:57 AM
It is the incident table in my case. I have also edited the code part.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2021 07:43 AM
Hello All,
I ran into issue and it was because I was missing the below line , which should have been added below the line that specifies the 'accept' header:
$headers.Add('Content-Type','application/json')
And a line like this was required below the line that specifies the http method:
$body = "{
`"sysparm_quantity`":`"1`",
`"variables`":{
`"requested_for`" : `"6816f79cc0a8016401c5a33be04be441`",
`"needed_by`" : `"today`"
}
}"
I ran into this issue because I decided to not test a endpoint in Service Now's Rest API Explorer program before generating the powershell code. In my case, I had to create a service catalog item using the API so providing the correct payload for that type of request allowed Service Now to generate the correct code for it.
Overall my code looked liked this:
# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "admin"
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
# Specify endpoint uri
$uri = "https://dev77880.service-now.com/api/sn_sc/servicecatalog/items/146c050a1b1130108e074196bc4bcb09/order_now"
# Specify HTTP method
$method = "post"
# Specify request body
$body = "{
`"sysparm_quantity`":`"1`",
`"variables`":{
`"requested_for`" : `"6816f79cc0a8016401c5a33be04be441`",
`"needed_by`" : `"today`"
}
}"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body
# Print response
$response.RawContent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 12:06 PM
This is old but if anyone tries to get incidents as JSON. This is a working piece
# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "admin"
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
# Specify endpoint uri
$uri = "https://yours.service-now.com/api/now/table/incident?sysparm_fields=number%2Csys_created_by%2Cstate%2Cactive%2Cimpact%2Cpriority%2Csys_class_name%2Cshort_description&sysparm_limit=100"
# Specify HTTP method
$method = "get"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
ConvertTo-Json $response.result | Out-File -FilePath .\incidents.json
# Print response