pythonrestservicenow

Service-Now API, python PUT snippet for assigning incident to me


I am working with https://developer.servicenow.com/dev.do#!/reference/api/utah/rest/c_TableAPI

and I get a list of incidents I need for the next step.

Next step is: I need to assign certain incidents to me. I wonder if anyone could help with service-now REST API.

I need an example for assigning a given incident to myself.

21/11/2023, when I try this snippet (according to https://developer.servicenow.com/dev.do#!/reference/api/utah/rest/c_TableAPI#table-PUT):

id_incident = '49akkkkkkkkkkkkkkkk3'
url = 'https://PPPPPPPPP.service-now.com/api/now/table/incident/{}'.format(id_incident)

user = "OOOOOOO"
pwd = "OOOOOOO"

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
data = {"assigned_to":"12cccccccccccccccccca"}

# Do the HTTP request
response = requests.put(url, auth=(user, pwd), headers=headers, data=data, timeout=None)
res = response.json()

I get this error printing res:

{'error': {'message': 'Exception while reading request',
  'detail': 'The payload is not valid JSON.'},
 'status': 'failure'}

Solution

  • After reading the API documentation, I believe that the API requests a JSON- formatted string in the body. Perhaps you could try the following :

    response = requests.put(url, auth=(user, pwd), headers=headers, json=data, timeout=None)