3

I am trying to login to a website and based on the REST API guide provided, I will be able to receive some data for an application.

The two steps required for me to enact are: 1. Send a HTTP post command for authentication. 2. Send a GET command for receiving the data.

When I send the post command using python requests, i receive the required json response showing my login rights. e.g. role admin.

However, when I perform the get command after, it doesn't retrieve the data but sends an HTML form showing that I require authentication even though I have authenticated already.

Has anyone encountered this and how will I be able to solve it?

I am working on this for a customer and as a result, cannot post the actual login I am using and url and will hence replace this with my name with the code I will display.

Thanks enter image description here

3
  • Check the API documentation. Probably you need to set an Authorization header or something like that.
    – Bart
    Commented Mar 1, 2018 at 15:45
  • Thanks Bart for the comment. The APi states that I must accept a cookie from the authentication for subsequent rest calls to keep the session alive.
    – Freddy A
    Commented Mar 1, 2018 at 15:49
  • Do you know how I can do this with requests?
    – Freddy A
    Commented Mar 1, 2018 at 15:49

1 Answer 1

3

You can take a look at the session object in the requests library. It's used to keep a session across multiple requests. https://3.python-requests.org/user/advanced/#session-objects

import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set?authcookie=123')
r = s.get('http://httpbin.org/cookies')
print(r.text)

This is a sample that uses a request session object to call a url that sets a cookie and then does another call with the same session (including the cookie). Just some basic example using http://httpbin.org/.

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Mar 2, 2018 at 11:35
  • @AndréKool added a sample
    – Bart
    Commented Mar 2, 2018 at 16:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.