call Rest API with python

1
0
-1

Hello

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

  1. import httplib2
  2. import urllib
  3. import json
  4.  
  5.  
  6.  
  7. def login(username, password):
  8. http = httplib2.Http()
  9. API = "/loginservice"
  10. URL = BONITA_URL + API
  11. body = {'username': username, 'password': password, 'redirect': 'false'}
  12. headers = {"Content-type": "application/x-www-form-urlencoded"}
  13. response, content = http.request(URL, 'POST', headers=headers, body=urllib.parse.urlencode(body))
  14. if response.status != 200:
  15. raise Exception("HTTP STATUS: " + str(response.status))
  16.  
  17.  
  18. return response['set-cookie']
  19.  
  20.  
  21. def listUsers(cookie):
  22. http = httplib2.Http()
  23. API = "/API/identity/user"
  24. params = "?p=0&c=10&o=lastname ASC&f=enabled=true"
  25. URL = BONITA_URL + API + params
  26. headers = {"Content-type": "application/json", 'Cookie': cookie}
  27. response, content = http.request(URL, 'GET', headers=headers)
  28. print(response.status)
  29. print(content)
  30. data = json.loads(content)
  31. return data
  32.  
  33.  
  34. def main():
  35. loginCookie = login("daniel", "daniel")
  36. print(loginCookie)
  37. users = listUsers(loginCookie)
  38. print(users)
  39.  
  40.  
  41. if __name__ == "__main__":
  42. main()

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

can someone tell what is wrong?

Comments

Submitted by Sean McP on Wed, 03/15/2017 - 07:13

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:

  1. 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

4 answers

1
+1
-1
This one is the BEST answer!

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"

1
0
-1

I have the same problem with my website: https://handmadewritings.com
I would be glad to manage this problem.

1
0
-1

true indeed in python both are the same >> docs.python.org/2.0/ref/strings.html

In plain English: String literals can be enclosed in matching single quotes (') or double quotes (")

some like me with my buycollegeessay.org project use single and double quotation marks to make it easier to differentiate between various strings

1
0
-1

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.

Comments

Submitted by danielftavares on Wed, 03/15/2017 - 09:58

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

Testing
Notifications