Using headers with the Python requests library's get method
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: Cool Puzzler LoFi
--
Chapters
00:00 Question
00:31 Accepted answer (Score 523)
00:47 Answer 2 (Score 56)
01:10 Answer 3 (Score 55)
02:17 Answer 4 (Score 9)
02:43 Thank you
--
Full question
https://stackoverflow.com/questions/6260...
Question links:
http://docs.python-requests.org/en/lates...
Accepted answer links:
[API]: https://requests.readthedocs.io/en/lates.../
Answer 2 links:
[This answer]: https://stackoverflow.com/a/36634227/819...
[Sessions also handle cookies.]: http://docs.python-requests.org/en/lates...
Answer 3 links:
[docs]: http://docs.python-requests.org/en/lates.../
Answer 4 links:
http://myhttpheader.com
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #httprequest #pythonrequests
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Cool Puzzler LoFi
--
Chapters
00:00 Question
00:31 Accepted answer (Score 523)
00:47 Answer 2 (Score 56)
01:10 Answer 3 (Score 55)
02:17 Answer 4 (Score 9)
02:43 Thank you
--
Full question
https://stackoverflow.com/questions/6260...
Question links:
http://docs.python-requests.org/en/lates...
Accepted answer links:
[API]: https://requests.readthedocs.io/en/lates.../
Answer 2 links:
[This answer]: https://stackoverflow.com/a/36634227/819...
[Sessions also handle cookies.]: http://docs.python-requests.org/en/lates...
Answer 3 links:
[docs]: http://docs.python-requests.org/en/lates.../
Answer 4 links:
http://myhttpheader.com
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #httprequest #pythonrequests
#avk47
ACCEPTED ANSWER
Score 598
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"})
ANSWER 2
Score 73
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'})
ANSWER 3
Score 58
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
Responseobject.Parameters:
- url – URL for the new
Requestobject.- 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.
ANSWER 4
Score 18
Go to http://myhttpheader.com
copy attributes - typically 'Accept-Language' and 'User-Agent'.
Wrap them in the dictionary:
headers = { 'Accept-Language' : content-copied-from-myhttpheader, 'User-Agent':content-copied-from-myhttpheader}pass headers in your request
requests.get(url=your_url,headers=headers)