Adding headers to requests module
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 Question
00:34 Accepted answer (Score 317)
01:14 Answer 2 (Score 74)
01:37 Thank you
--
Full question
https://stackoverflow.com/questions/8685...
Question links:
http://pypi.python.org/pypi/requests
Accepted answer links:
http://docs.python-requests.org/en/lates.../
Answer 2 links:
http://docs.python-requests.org/en/lates...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #httpheaders #pythonrequests
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 Question
00:34 Accepted answer (Score 317)
01:14 Answer 2 (Score 74)
01:37 Thank you
--
Full question
https://stackoverflow.com/questions/8685...
Question links:
http://pypi.python.org/pypi/requests
Accepted answer links:
http://docs.python-requests.org/en/lates.../
Answer 2 links:
http://docs.python-requests.org/en/lates...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #httpheaders #pythonrequests
#avk47
ACCEPTED ANSWER
Score 334
From http://docs.python-requests.org/en/latest/user/quickstart/
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
You just need to create a dict with your headers (key: value pairs where the key is the name of the header and the value is, well, the value of the pair) and pass that dict to the headers parameter on the .get or .post method.
So more specific to your question:
headers = {'foobar': 'raboof'}
requests.get('http://himom.com', headers=headers)
ANSWER 2
Score 80
You can also do this to set a header for all future gets for the Session object, where x-test will be in all s.get() calls:
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
from: http://docs.python-requests.org/en/latest/user/advanced/#session-objects