Using headers with the Python requests librarys get method

Using headers with the Python requests librarys get method

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

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.

Using headers with the Python requests librarys get method

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.

Leave a Reply

Your email address will not be published. Required fields are marked *