Using headers with the Python requests library’s get method

So I recently stumbled upon this great library for handling HTTP requests in Python; found here http://docs.python-requests.org/en/latest/index.html.

I love working with it, but I can’t figure out how to add headers to my get requests. Help?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

According to the API, the headers can all be passed in with requests.get():

import requests
r=requests.get("http://www.example.com/", headers={"Content-Type":"text"})

Method 2

Seems pretty straightforward, according to the docs on the page you linked (emphasis mine).

requests.get(url, params=None, headers=None, cookies=None, auth=None,
timeout=None)

Sends a GET request.
Returns Response object.

Parameters:

  • url – URL for the new
    Request object.
  • params – (optional)
    Dictionary of GET Parameters to send
    with the Request.
  • headers – (optional)
    Dictionary of HTTP Headers to send
    with the Request.
  • cookies – (optional)
    CookieJar object to send with the
    Request.
  • auth – (optional) AuthObject
    to enable Basic HTTP Auth.
  • timeout –
    (optional) Float describing the
    timeout of the request.

Method 3

This answer taught me that you can set headers for an entire session:

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'})

Bonus: Sessions also handle cookies.

Method 4

  1. Go to http://myhttpheader.com
  2. copy attributes – typically ‘Accept-Language’ and ‘User-Agent’.
  3. Wrap them in the dictionary:
    headers = { 'Accept-Language' : content-copied-from-myhttpheader,
                'User-Agent':content-copied-from-myhttpheader}
    
  4. pass headers in your request
    requests.get(url=your_url,headers=headers)
    


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x