REST not working on instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2015 04:27 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2015 07:11 AM
Hi Allan,
Even:
>>> response.text
is not printing anything for you?
Script works for me in both python 2.7 and 3.4.
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2015 07:24 AM
Hi Sergiu,
response.text is empty (did a len(response.text) and verified that it's 0).
Could this be something to do with the config on our SN instance?
Regards,
Allan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2015 07:35 AM
Hi Allan,
The fact that you see incident being created in the instance makes me think that script works indeed.
The fact that you don't get any data back (while you get an HTTP 201) makes me think something is stripping the data. Do you have a firewall or a proxy in between? Are you able to test the script from a different network (not for example office network)?
If you give me your instance name (if it's a subproduction one), I could try a test from my side and would let you know if I get same issue or not.
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2015 07:54 AM
Hi Sergiu,
I've already tested this from my home connection which means that it's definitely not a firewall/proxy.
I'm using https://informadev.service-now.com
Regards,
Allan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2015 08:42 AM
Hi Allan,
Script works from my side:
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> import json as simplejson
>>> url = 'https://informadev.service-now.com/api/now/table/incident'
>>> user = 'SXXXXX'
>>> pwd = 'XXXXXX'
>>> headers = {"Content-Type":"application/json", "Accept":"application/json"}
>>> response = requests.post(url, auth=(user, pwd), headers=headers, data='{"short_description":"Test"}')
>>> if response.status_code != 201:
... print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
... raise SystemExit
...
>>> print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())
('Status:', 201, 'Headers:', {'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'set-cookie': ...., 'Response:', {u'result': {u'sla_due': u'', u'follow_up': u'', u'activity_due': .......)
>>>
An incident has been created on your instance but I have truncated the response json payload due to security reasons.
I still think it is something with character encoding.
I will have another look on Monday if you can't figure it out by then.
Regards,
Sergiu