Monday, March 28, 2022

[SOLVED] Python Requests : How to get response cookie from 302 redirect

Issue

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

I was trying to solve this today and found a relevant bug in requests.

Using JohnCC330's solution of turning off auto_redirect worked for me:

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

if res.status_code == 302: # expected here
    jar = res.cookies
    redirect_URL2 = res.headers['Location']
    res2 = requests.get(redirect_URL2, cookies=jar)
    # res2 is made with cookies collected during res' 302 redirect

Hope this helps,

Bob



Answered By - BobRz
Answer Checked By - Mildred Charles (WPSolving Admin)