Using headers with the Python requests library's get method


Question

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?

1
111
4/7/2012 11:32:46 AM

Accepted Answer

According to the api, the headers can all be passed in using requests.get:

r=requests.get("http://www.example.com/", headers={"content-type":"text"})
198
2/21/2018 8:59:20 PM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon