'Python Requests : How to get response cookie from 302 redirect

I'm trying to make an automation that will send the http requests to follow or unfollow a user through an instagram api, right now I'm using the Python Requests module and Im trying to do it through the site 'http://pikore.com'. My current code is is :

import requests
from requests.auth import HTTPBasicAuth

s = requests.Session()
s.get('http://pikore.com')

print(s.cookies)
s.get('http://www.pikore.com/session/new?from=%2F', auth=HTTPBasicAuth('USERNAME', 'USERSECRET'))
pikore_session = s.cookies['_pikore_session']
print(s.cookies)
s.get('http://pikore.com')
print(s.cookies)

cookies = {
'_pikore_session': pikore_session,
'token': 'BAhJIjcyNTY5NDczOTIuZWIxM2FlYi41Mjc3ZTI4NjI4ZDM0NDRlOGRjNWNjZmFjNWU3YmJmYgY6BkVU--9b52c6305368f28c83ffc288e9d55e94b3e864db',
}

headers = {
    'Host': 'www.pikore.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
    'Referer': 'http://www.pikore.com/nike',
}

print(s.get('http://www.pikore.com/follow/13460080', headers=headers, cookies=cookies))
s.get('http://www.pikore.com/')
print(s.cookies)

So this works and will carry out the given request, the only thing is, the authorization line will only get the '_pikore_session' cookie, but not the token cookie, which I want to get. After you log in through the instagram authorization redirect, it will redirect you three times and then finally you'll land on the website, logged in, but on the third redirect, I can see that it outputs the 'token' response cookie, I want to someone get that so that I dont have to go and get it each time manually.



Solution 1:[1]

Alternatively you can keep the automatic redirect support and iterate through the response history to merge the cookies. This code will return all cookies as a dict. This includes cookies that were only returned in the redirect.

def get_cookies_from_response(response: requests.Response) -> dict:
    """Given a response, return all cookies as a dict."""
    cookies = response.cookies.get_dict()
    # If there was a redirect, the cookies we need may be in a previous response
    for r in response.history:
        cookies.update(r.cookies.get_dict())  # Merge cookies
        
    return cookies

res = requests.post(
    host,
    data={'somefield':'my value'},
    allow_redirects=True)

cookies = get_cookies_from_response(res)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Russell