How to avoid ConnectionError ServiceNow REST API

bhain
Kilo Contributor

Hello! I'm trying to connect to the API using a basic Python script from the document, and login credentials that work in my web browser and having no luck.

Getting:  ConnectionError: HTTPSConnectionPool(host='mycompanydev.service-now.com', port=443): Max retries exceeded with url: /api/now/table/incident?sysparm_limit=10 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fe64850f4a8>: Failed to establish a new connection: [Errno 110] Connection timed out',))

Wondering if anyone knows the source of this issue/ how to troubleshoot.

Using a basic table API script from the Servicenow Rest API explorer.

 

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://mycompanydev.service-now.com/api/now/table/incident?sysparm_limit=10'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'myusername'
pwd = 'mypassword'

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

# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers , timeout=None)

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

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

 

EDIT: Works for me on desktop, but fails using the VPC hosted Juypter Lab I was running it in. It might have to do with the VPC server and company security. 

Really need the VPC hosted one to be able to use it effectively at company though, so I guess that's the issue,

When I set up PIP for desktop python, I put in some extra code to get through a "proxy", not sure what that means.

8 REPLIES 8

ben_knight
Kilo Guru

Not sure what your specific issue is but I have tested the following script and it is working as expected. I have grabbed this from the REST API explorer code. Check it out to see if you are missing something that this has.

 

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://<MY INSTANCE>.service-now.com/api/now/table/incident?sysparm_limit=10'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

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

# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )

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

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

 

If you post your code I may be able to help more.

 

EDIT: Perhaps make sure your developer instance is awake as if it is asleep you may receive connection errors.

bhain
Kilo Contributor

I tried this code out with both the Admin placeholder as well as my credentials that work when I use a browser based connection. No luck so far 😞 

I'm not part of the SN admin team so I can't wake or sleep the instance, but it's definitely running in the sense that I can go to the URL and navigate the instance.

Anil Shewale
Mega Guru

Hi bhain

 

Have you tried removing the timeout?

Source: https://stackoverflow.com/questions/24045265/python-requests-httpconnectionpool-and-max-retries-exce...

 

If it help mark helpful or correct.

Thanks and regards

Anil

I tried

response = requests.get(url, auth=(user, pwd), headers=headers , timeout=None)

No luck, is there a different way I should be coding this?