Using headers with the Python requests library's 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"})
Can you get Request Headers with python request library?
The response object contains a request object that is the request which produced the reponse.
This requests.models.PreparedRequest
object is accessible through the request
property of the response object, its header are in the property headers
of the request object.
See this example:
>>> import requests
>>> r = requests.get("http://google.com")
>>> r.request.headers
{'Connection': 'keep-alive', 'User-Agent': 'python-requests/2.22.0', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate'}
Adding headers to requests module
From http://docs.python-requests.org/en/latest/user/quickstart/
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
r = requests.post(url, 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('http://himom.com', headers=headers)
How can I specify the exact http method with python requests?
requests
uses urllib3
under the hood.
You have to craft the request yourself because of non of the clients [urlib, requests, http.client] won't allow you to insert a control character by design.
You can use a plain socket for this
msg = 'GET / HTTP/1.1\r\n\r\n'
s = socket.create_connection(("vulnerable-website.com", 80))
with closing(s):
s.send(msg)
buf = ''.join(iter(partial(s.recv, 4096), ''))
Sending User-agent using Requests library in Python
The user-agent
should be specified as a field in the header.
Here is a list of HTTP header fields, and you'd probably be interested in request-specific fields, which includes User-Agent
.
If you're using requests v2.13 and newer
The simplest way to do what you want is to create a dictionary and specify your headers directly, like so:
import requests
url = 'SOME URL'
headers = {
'User-Agent': 'My User Agent 1.0',
'From': 'youremail@domain.example' # This is another valid field
}
response = requests.get(url, headers=headers)
If you're using requests v2.12.x and older
Older versions of requests
clobbered default headers, so you'd want to do the following to preserve default headers and then add your own to them.
import requests
url = 'SOME URL'
# Get a copy of the default headers that requests would use
headers = requests.utils.default_headers()
# Update the headers with your custom ones
# You don't have to worry about case-sensitivity with
# the dictionary keys, because default_headers uses a custom
# CaseInsensitiveDict implementation within requests' source code.
headers.update(
{
'User-Agent': 'My User Agent 1.0',
}
)
response = requests.get(url, headers=headers)
Related Topics
Dynamic Instantiation from String Name of a Class in Dynamically Imported Module
Automatically Initialize Instance Variables
How to Increment Datetime by Custom Months in Python Without Using Library
How to Install Python Windows Packages into Virtualenvs
Iterate Over Object Attributes in Python
Log All Requests from the Python-Requests Module
Deleting Multiple Elements from a List
Python Find Elements in One List That Are Not in the Other
How to Perform Element-Wise Multiplication of Two Lists
How to Convert a File into a Dictionary
Getting Number of Elements in an Iterator in Python
Parameterized Queries with Psycopg2/Python Db-API and Postgresql
How Find Specific Data Attribute from HTML Tag in Beautifulsoup4
R Markdown: How to Make Rstudio Display Python Plots Inline Instead of in New Window