Reputation: 21
I am pulling a lot of data from api. Like this.
session = requests.Session()
response = session.post(url, 'auth'), verify=False, json={CREDENTIALS HERE})
head = {'Authorization': 'Bearer %s' % (response.json()['token'])}
response = requests.get(url, headers=head)
Is there any good way to save/cache the header and not get new token all the time?
Upvotes: 2
Views: 3192
Reputation: 413
You can make one request to the site using sessions and then do session.headers to get the headers that was sent back from the site and save it to a variable.
serverHeaders = r.headers
#r.headers is below
{'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':
'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':
'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',
'server': 'Apache', 'last-modified': 'Wed, 13 Jun 2012 01:33:50 GMT',
'connection': 'close', 'cache-control': 'private, s-maxage=0, max-age=0,
must-revalidate', 'date': 'Thu, 14 Jun 2012 12:59:39 GMT', 'content-type':
'text/html; charset=UTF-8', 'x-cache-lookup': 'HIT from cp1006.eqiad.wmnet:3128,
MISS from cp1010.eqiad.wmnet:80'}
https://requests.readthedocs.io/en/master/user/advanced/#request-and-response-objects
In the documentation they have a lot of code examples for it.
Upvotes: 1