Solved

API - Python

  • 18 April 2023
  • 3 replies
  • 113 views

Hi All,

Has anyone successfully created a Python script to query the Sysaid API? I’ve been struggling to get this working … POSTMAN is working fine though. 
 

Just wondered if anyone had a sample script i could look at. 

 

 

Thanks,

 

icon

Best answer by cary512 31 May 2023, 23:39

View original

3 replies

Hi @infoMarlee

In general, creating Python scripts for API calls is not in our scope of support. 

However, if you give us an example of what you’re trying to achieve, we’ll try to ask around internally and see if there’s anything we can help you with.  

 

All the best :)

I just posted some python code a few hours ago regarding a problem with attachments.  That could get you started.  I didn’t include a login routine so you’re going to get a 401 response every time, until you handle that.  You could merge that code with the code below and should be able to get it working.  I extracted this from a larger codebase so there’s probably some bugs.  At the very least I think I used “/tmp” for the cookie path in that other code instead of the account-specific path found here.  Good luck!

 

import urllib

username = "my-sysaid-username"

rawpass = "my-plaintext-sysaid-password"

 

def login():

    global headers

    headers = { 'Content-Type': 'application/json' }

    logininfo = {'user_name': username, 'password': urllib.parse.quote_plus(rawpass)}

    saurl = "https://%s.sysaidit.com/api/v1/login" %account

    response = session.post(url=saurl, headers=headers, json=logininfo)

    if response['status_code'] == 200:

        # login successful - store the session id

        scresponse = save_cookie(response['headers'])

        return scresponse

    return False # login failed

 

def save_cookie(rheaders):

    global headers

    cookiestr = str(rheaders['set-cookie'])

    cookie = cookiestr.split(';')[0]

    headers = { 'Cookie': cookie, 'Content-Type': 'application/json' }

    os.makedirs("/tmp/sysaid/%s/state" %account, exist_ok=True)

    with open('/tmp/sysaid/%s/state/jsessionid' %account, 'w') as fd:

        fd.write(cookie)

        fd.write('\n')

        fd.close()

        return True

    return False

Userlevel 3
Badge +1

Hi @infoMarlee

Please let us know whether @cary512’s response helped you out and if it resolved your issue. 

 

All the best :)

Reply