1

I'm relatively new to Python so excuse any errors or misconceptions I may have. I've done hours and hours of research and have hit a stopping point.

I'm using the Requests library to pull data from a website that requires a login. I was initially successful logging in through through a session.post,(payload)/session.get. I had a [200] response. Once I tried to view the JSON data that was beyond the login, I hit a [403] response. Long story short, I can make it work by logging in through a browser and inspecting the web elements to find the current session cookie and then defining the headers in requests to pass along that exact cookie with session.get

My questions is...is it possible to set/generate/find this cookie through python after logging in? After logging in and out a few times, I can see that some of the components of the cookie remain the same but others do not. The website I'm using is garmin connect.

Any and all help is appreciated.

user13546612
  • 11
  • 1
  • 2

1 Answers1

1

If your issue is about login purposes, then you can use a session object. It stores the corresponding cookies so you can make requests, and it generally handles the cookies for you. Here is an example:

s = requests.Session() 
# all cookies received will be stored in the session object

s.post('http://www...',data=payload)
s.get('http://www...')

Furthermore, with the requests library, you can get a cookie from a response, like this:

url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies

But you can also give cookie back to the server on subsequent requests, like this:

url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)

I hope this helps!

Reference: How to use cookies in Python Requests

georgekrax
  • 1,065
  • 1
  • 11
  • 22
  • When I use this process(cookie jar), the cookie response I receive only has maybe 1/8 the info that the browser cookie has – user13546612 May 15 '20 at 14:09
  • When you request a cookie, you get the corresponding cookie, not all of them. – georgekrax May 15 '20 at 17:16
  • Shouldn’t I be able to use the corresponding cookie and pass that along? When I do that, I get the [403] response – user13546612 May 15 '20 at 17:29
  • Then something is wrong with the **Authorization**. **403** resolves to **Forbidden** error, please check that please first. I do not think it is a problem with your requests. – georgekrax May 15 '20 at 17:37