Adding headers to requests module


Earlier I used httplib module to add a header in the request. Now I am trying the same thing with the requests module.

This is the python request module I am using:

How can I add a header to and request.get(). Say I have to add foobar key in each request in the header.

This question is tagged with python http-headers python-requests

~ Asked on 2011-12-31 02:02:08

The Best Answer is



url = ''
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}

r =, 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('', headers=headers)

~ Answered on 2011-12-31 02:07:16


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('', headers={'x-test2': 'true'})


~ Answered on 2016-04-14 21:20:45

Most Viewed Questions: