Hello dear Stack users,
I am trying to login to a webservice via json exchange and trying to implement the auth mechanism as you can see here
import requests
class Testauth(requests.auth.AuthBase):
"""Implements a custom authentication scheme."""
def __init__(self, email, passwd):
loginjson = {"Data":{"EMAIL":email,"PASSWORD":passwd}}
req = requests.post('https://httpbin.org/post', json=loginjson)
self.token = self.token = req.json()['data'] #orig service has : ['Data'][0]['SESSION_ID']
def __call__(self, r):
"""Attach an API token to a custom auth header."""
r.prepare_cookies({'SESSION_ID' : f'{self.token}'}) # Python 3.6+
return r
curSession = requests.Session()
curSession.auth=Testauth('email','pass')
page = curSession.get('https://httpbin.org/get')
print(curSession.cookies)
print(page.content.decode('utf-8'))
My expectation was that since it did authentication while accessing the /get (with the auth method) curSession should contain cookies
But checking cursession.cookies shows none, only the request contains cookies.
Here shows that session should persist cookies between requests but it does not, where am I doing wrong in here
Tl;Dr:
When using sessions, implementing custom auth doesnt persist cookie in auth, gets new auth cookie everytime