Search code examples
pythonsoapservicenow

ServiceNow - How to use SOAP to download reports


I need to automate download of reports from serviceNow.

I've been able to automate it using python and selenium and win32com by following method.
https://test.service-now.com/sys_report_template.do?CSV&jvar_report_id=92a....7aa

And using selenium to access serviceNow as well as modify firefox default download option to dump the file to a folder on windows machine.

However, Since all of this may be ported to a linux server , we would like to port it to SOAP or CURL.

I came across serviceNow libraries for python here.

I tried it out and following code is working if I set login , password and instance-name as listed at the site using following from ServiceNow.py

class Change(Base):
    __table__ = 'change_request.do'

and following within clientside script as listed on site.

# Fetch changes updated on the last 5 minutes
changes = chg.last_updated(minutes=5)
#print changes client side script.

for eachline in changes:
    print eachline

However, When I replace URL with sys_report_template.do, I am getting error

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\SOAPpy\Parser.py", line 1080, in _parseSOAP
    parser.parse(inpsrc)
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "C:\Python27\Lib\xml\sax\xmlreader.py", line 125, in parse
    self.close()
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 220, in close
    self.feed("", isFinal = 1)
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 214, in feed
    self._err_handler.fatalError(exc)
  File "C:\Python27\Lib\xml\sax\handler.py", line 38, in fatalError
    raise exception
SAXParseException: <unknown>:1:0: no element found

Here is relevent code

from servicenow import ServiceNow
from servicenow import Connection
from servicenow.drivers import SOAP

# For SOAP connection
conn = SOAP.Auth(username='abc', password='def', instance='test')

rpt  = ServiceNow.Base(conn)
rpt.__table__ = "sys_report_template.do?CSV"

#jvar_report_id replaced with .... to protect confidentiality
report = rpt.fetch_one({'jvar_report_id': '92a6760a......aas'})

for eachline in report:
    print eachline

So, my question is , what can be done to make this work? I looked on web for resources and help, but didn't find any.

Any help is appreciated.


Solution

  • After much research I was able to use following method to get report in csv format from servicenow. I thought I will post over here in case anyone else runs into similar issue.

    import requests
    import json
    
    # Set the request parameters
    url= 'https://myinstance.service-now.com/sys_report_template.do?CSV&jvar_report_id=929xxxxxxxxxxxxxxxxxxxx0c755'
    user = 'my_username'
    pwd = 'my_password'
    
    # Set proper headers
    headers = {"Accept":"application/json"}
    
    # Do the HTTP request
    response = requests.get(url, auth=(user, pwd), headers=headers )
    response.raise_for_status()
    print response.text
    

    response.text now has report in csv format.
    I need to next figure out, how to parse the response object to extract csv data in correct format.
    Once done, I will post over here. But for now this answers my question.