call Rest API with python

Hello

I’m trying to call the rest REST API with python. Here is the code:

import httplib2 import urllib import json

BONITA_URL = “http://localhost:8080/bonita/

def login(username, password):
http = httplib2.Http()
API = “/loginservice”
URL = BONITA_URL + API
body = {‘username’: username, ‘password’: password, ‘redirect’: ‘false’}
headers = {“Content-type”: “application/x-www-form-urlencoded”}
response, content = http.request(URL, ‘POST’, headers=headers, body=urllib.parse.urlencode(body))
if response.status != 200:
raise Exception("HTTP STATUS: " + str(response.status))

return response['set-cookie']

def listUsers(cookie):
http = httplib2.Http()
API = “/API/identity/user”
params = “?p=0&c=10&o=lastname ASC&f=enabled=true”
URL = BONITA_URL + API + params
headers = {“Content-type”: “application/json”, ‘Cookie’: cookie}
response, content = http.request(URL, ‘GET’, headers=headers)
print(response.status)
print(content)
data = json.loads(content)
return data

def main():
loginCookie = login(“daniel”, “daniel”)
print(loginCookie)
users = listUsers(loginCookie)
print(users)

if name == “main”:
main()

The login works, but line “print(response.status)” prints the 505 response code.

can someone tell what is wrong?

headers = {“Content-type”: “application/json”, ‘Cookie’: cookie}

are you sure about the correct " and ’ usage? should it be

headers = {“Content-type”: “application/json”, “Cookie”: cookie}

regards
Seán

PS: As this reply offers an answer your question, and if you like it, please Mark UP and/or as Resolved.

I got it!

I can’t send a whitspace on url params. I just changed:

params = “?p=0&c=10&o=lastname ASC&f=enabled=true”
to
params = “?p=0&c=10&o=lastname%20ASC&f=enabled%3dtrue”

A Tip on displaying CODE/LOGS correctly in Posts:

Do not use the Supplied Buttons above, for some reason they refuse to work correctly, and despite bringing it to Bonitasofts attention, it’s low priority.

To Show Code/Logs correctly use

< code >
your code/log
< /code >

removing the spaces to allow the showing of code/logs correctly as here:

your code/log

You should still be able to edit your post to ensure the correct formatting of the code to help us understand it easier.

Thanks and regards
Seán

thanks for reply Sean,

As far as i know, both are equals in python, aren’t they?
I have changed the code anyway and got the same error.

ps: i’m doing this just for fun. i have no professional or academic interest