Reputation: 1583
I am trying to log in to a page and access another link in the page.
I get a "405 Not Allowed" error from this attempt:
payload={'username'=<username>,'password'=<password>}
with session() as s:
r = c.post(<URL>, data=payload)
print(r)
print(r.content)
I checked the post method details using Chrome developer tools and found a URL that appeard to be an API endpoint. I posted to that URL with the payload and it seemed to work; I got a response similar to what I could see in the developer.
Unfortunately, when trying to 'get' another URL after logging in, I am still getting the content from the login page. Why is the login not sticking? Should I use cookies? How?
Upvotes: 143
Views: 419497
Reputation: 11
Unfortunately, the requests
does not allow cookies
to be passed in the headers
, so this method does not work. Сookies should be passed as a separate argument r = requests.get(url, cookies=cookies)
Upvotes: 1
Reputation: 14328
Summary (@Freek Wiekmeijer, @gtalarico) other's answer:
authentication
, then can access, otherwise 401
=Unauthorized
authentication
=grant access
method are:
cookie
auth header
Basic xxx
Authorization xxx
cookie
in requests
to authcookie
in headers
cookie
by requests
's
session
to auto manage cookiesresponse.cookies
to manually set cookiesrequests
's session
auto manage cookiescurSession = requests.Session()
# all cookies received will be stored in the session object
payload={'username': "yourName",'password': "yourPassword"}
curSession.post(firstUrl, data=payload)
# internally return your expected cookies, can use for following auth
# internally use previously generated cookies, can access the resources
curSession.get(secondUrl)
curSession.get(thirdUrl)
requests
's response.cookies
payload={'username': "yourName",'password': "yourPassword"}
resp1 = requests.post(firstUrl, data=payload)
# manually pass previously returned cookies into following request
resp2 = requests.get(secondUrl, cookies= resp1.cookies)
resp3 = requests.get(thirdUrl, cookies= resp2.cookies)
Upvotes: 18
Reputation: 195
As others noted, Here is an example of how to add cookies as string variable to the headers parameter -
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
"cookie": "_fbp=fb.1.1654447470850.2143140577; _ga=GA1.2.1...",
}
response = requests.get(url, headers=headers)
Upvotes: 6
Reputation: 4940
From the documentation:
get cookie from response
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies
{'example_cookie_name': 'example_cookie_value'}
give cookie back to server on subsequent request
url = 'http://httpbin.org/cookies'
cookies = {'cookies_are': 'working'}
r = requests.get(url, cookies=cookies)`
Upvotes: 111
Reputation: 4689
You can use a session object. It stores the cookies so you can make requests, and it handles the cookies for you
s = requests.Session()
# all cookies received will be stored in the session object
s.post('http://www...',data=payload)
s.get('http://www...')
Docs: https://requests.readthedocs.io/en/master/user/advanced/#session-objects
You can also save the cookie data to an external file, and then reload them to keep session persistent without having to login every time you run the script:
How to save requests (python) cookies to a file?
Upvotes: 176