REST not working on instance

allanmullan
Kilo Explorer

I'm looking at moving an application that I wrote to integrate Nagios with Service Now using Python from SOAP to REST and have run into a bit of a stumbling block.

Basically, going by the example on the SN docs, the below should work:

import requests
import json

# Set the request parameters
url = 'https://xxx.service-now.com/api/now/table/incident'
user = 'xxx'
pwd = 'xxx'

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

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers , data = '{"short_description": "test"}')

# Check for HTTP codes other than 200
if response.status_code != 201:
       print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
       raise SystemExit

# Decode the JSON response into a dictionary and use the data
print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())

But instead I get:

 File "./REST.py", line 23, in         print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())
   File "/usr/lib/python2.7/site-packages/requests/models.py", line 733, in json
       return json.loads(self.text, **kwargs)
   File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
       return _default_decoder.decode(s)
   File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
       obj, end = self.raw_decode(s, idx=_w(s, 0).end())
   File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
       raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I've double checked and the user account definitely has REST permissions and GET works absolutely fine. I've also tried putting all required information into the data input but it makes no difference. Also, I tried using the servicenow_rest library available from pip it seems I'm just not getting no return data.

Is this just something odd with the ServiceNow instance? Using the Chrome extension works for REST queries so guessing not?

Cheers,

Allan

28 REPLIES 28

Hi Sergiu,



I just printed the response.headers and I see the following:


CaseInsensitiveDict({'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'content-type': 'application/json'})


(some   items removed from dict where not required)



Thought that might be the problem but I see that the content-encoding on your request headers is the same.



Seems a bit odd to me that using GET works perfectly, it's just POST / PUT that seems to not work.



I'll carry on investigating and let you know if I figure it out.



Cheers


Allan


OK, just tried using PUT and that works perfectly.



I checked response.text on both GET and PUT and both are returning JSON, whereas POST is returning nothing.


Allan,



Does it works for "POST" if you use Chrome extension for REST?


I am still puzzled about the fact it works for GET/PUT but not POST.



Regards,


Sergiu


Hey Sergiu,



Just tested using the Advanced REST Client in Chrome and, as expected, PUT/GET worked fine but POST came back with "Response does not contain any data." which is what I suspected it would do...



Regards,


Allan


sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Allan,



At this point we are sure that it's not your python script or browser not being able to decode the data.


Are you able to run the script/browser extension from another PC to see if you get same results?



Regards,


Sergiu