Getting the error "Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type" when trying to run powershell file in MID server

Akshatha Ballal
Tera Expert

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

4 REPLIES 4

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

What is that 

undefined

in the URL?

Thanks,
Ashutosh

It is the incident table in my case. I have also edited the code part.

Steven28
Kilo Contributor

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

 

Balan Mahadevan
Tera Contributor

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