- How To Get / Set Http Headers, Cookies And Manage Sessions Use Python Requests Module
- 1. Get / Set HTTP Headers Use Python Requests Module.
- 1.1 Get Server Response HTTP Headers.
- 1.2 Add Custom Headers To HTTP Request.
- 2. Get / Set Cookies Use Python Requests Module.
- 2.1 Python Get HTTP Cookies.
- 2.2 Python Send HTTP Cookies To Web Server.
- 3. Use Session In Python Requests Module.
- Using Headers with Python requests
- Understanding HTTP Headers and How to Use Them
- How to Pass HTTP Headers into a Python requests GET Request
- How to Pass HTTP Headers into a Python requests POST Request
- How to Check Headers Returned in a Python requests Response Object
- Conclusion
- Additional Resources
How To Get / Set Http Headers, Cookies And Manage Sessions Use Python Requests Module
In the previous article How To Use Python Requests Module To Send Get Or Post Request Example, we have learned how to install and use the python requests module to send HTTP Get and Post request to a web server. It also tells you how to post form data or pass query string parameters use the python requests module. This article will tell you how to use the python requests module to manage HTTP headers, cookies, and sessions.
1. Get / Set HTTP Headers Use Python Requests Module.
1.1 Get Server Response HTTP Headers.
Python requests module’s headers property is used to get HTTP headers. The headers property is a dictionary-type object, you should provide the header name to get the header value.
>>> import requests >>> response = requests.get("http://www.dev2qa.com") >>> response.headers['content-type'] 'text/html; charset=UTF-8'
If you want to get all headers, just call response.headers , it will list out all HTTP response headers in a JSON format string.
1.2 Add Custom Headers To HTTP Request.
import requests # Create a python dictionary object to save custom http headers. custom_header = # Pass above custom_header dictionary object to requests module's get function's headers parameter. response = requests.get('https://www.dev2qa.com', headers=custom_header)
2. Get / Set Cookies Use Python Requests Module.
2.1 Python Get HTTP Cookies.
- Get all HTTP response cookies by invoke response.cookies property. This property is an instance of requests.cookies.RequestsCookieJar class.
>>> import requests >>> response = requests.get(‘http://www.dev2qa.com’) >>> response.cookies
>>> for cookie in response.cookies: . print('cookie domain = ' + cookie.domain) . print('cookie name = ' + cookie.name) . print('cookie value = ' + cookie.value) . print('*************************************') . cookie domain = .dev2qa.com cookie name = __cfduid cookie value = d8dccd301af7c19f3286442254fe10aa51560824472 ************************************* cookie domain = .dev2qa.com cookie name = active_template::80464 cookie value = pub_site.1560824472 ************************************* .
>>> for item in rsponse.cookies.items(): . print(item) . ('__cfduid', 'd0c1cc559af0893eb31cd23bb9bc22a261560866202') ('active_template::80464', 'pub_site.1560866203') ('ezCMPCCS', 'true') ('ezepvv', '92') ('ezoab_80464', 'mod31') ('ezoadgid_80464', '-1') ('ezopvc_80464', '1') ('ezoref_80464', '') ('ezovid_80464', '1774096901') ('ezovuuid_80464', '2d40f1eb-b006-4540-75cb-09a37a3b8387') ('ezovuuidtime_80464', '1560866206') ('lp_80464', 'https://www.dev2qa.com/?user_name=jerry&password=jerry') >>> for item in cookies.iterkeys(): . print(item) . __cfduid active_template::80464 ezCMPCCS ezepvv ezoab_80464 ezoadgid_80464 ezopvc_80464 ezoref_80464 ezovid_80464 ezovuuid_80464 ezovuuidtime_80464 lp_80464
# Get cookie value by provided cookie name. >>> response.cookies['ezCMPCCS'] 'true'
# Use get_dict function to get related domain's cookies. >>> response.cookies.get_dict('.dev2qa.com')
>>> response.cookies.list_domains() ['.dev2qa.com']
2.2 Python Send HTTP Cookies To Web Server.
# Import python requests module. >>> import requests >>> # Set url value. >>> url = 'https://www.dev2qa.com' >>> # Create a dictionary object. >>> cookies = dict(name='jerry', password='888') >>> # Use python requests module to get related url and send cookies to it with cookies parameter. >>> response = requests.get(url, cookies=cookies)
>>> import requests >>> >>> url = 'https://www.dev2qa.com' >>> # Create a RequestsCookieJar object. >>> cookies_jar = requests.cookies.RequestsCookieJar() >>> # Add first cookie, the parameters are cookie_key, cookie_value, cookie_domain, cookie_path. >>> cookies_jar.set('name', 'jerry', domain='dev2qa.com', path='/cookies') Cookie(version=0, name='name', value='jerry', port=None, port_specified=False, domain='dev2qa.com', domain_specified=True, domain_initial_dot=False, path='/cookies', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest=, rfc2109=False) >>> # Add second cookie. >>> cookies_jar.set('password', 'jerry888', domain='dev2qa.com', path='/cookies') Cookie(version=0, name='password', value='jerry888', port=None, port_specified=False, domain='dev2qa.com', domain_specified=True, domain_initial_dot=False, path='/cookies', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest=, rfc2109=False) >>> # Get url with cookie parameter. >>> response = requests.get(url, cookies=cookies_jar)
3. Use Session In Python Requests Module.
Python requests module’s session() method will return a request.sessions.Session object, then all the later operations ( such as to browse a related URL page ) on this session object will use one same session.
>>> import requests >>> # Call requests module’s session() method to return a requests.sessions.Session object. >>> session = requests.session() >>> >>> print(session)
The returned request.sessions.Session objects provide a lot of attributes and methods for you to get related headers, cookie value in the same session. The session object also provides a get method to request a web page by URL. Below is an example of how to use the python requests module session object.
# Show all headers and cookies in this session. >>> session.headers >>> >>> session.cookies>>> # Use this session object to get a web page by url. >>> response = session.get('http://www.dev2qa.com') >>> # When above browse web page process complete, this session has cookies. >>> session.cookies >> >>> custom_header = # Get web page url and send custom headers. >>> >>> response = session.get('http://www.dev2qa.com', headers=custom_header)
The below link will show you python requests module session class source code, you can read it if you need it.
Using Headers with Python requests
In this tutorial, you’ll learn how to use custom headers with the Python requests library. HTTP headers allow you to send additional information to a server and allow the server to provide additional information back to you. Being able to work with headers allows you to, for example, authenticate yourself when working with APIs or inform the request which content type your application is expecting.
By the end of this tutorial, you’ll have learned:
- What HTTP headers are and what they are used for
- How to pass custom headers in a Python requests request, such as GET or POST requests
- How to see headers that are returned in a Python requests response
Understanding HTTP Headers and How to Use Them
HTTP headers allow the client and server to pass additional information while sending an HTTP request or receiving a subsequent response. These headers are case-insensitive, meaning that the header of ‘User-Agent’ could also be written as ‘user-agent’ .
Additionally, HTTP headers in the requests library are passed in using Python dictionaries, where the key represents the header name and the value represents the header value.
HTTP headers are useful for providing information that you expect the server to tailor for you. For example, they can be used to indicate the allowed or preferred formats of a response you’d like back. Similarly, headers can be used to provide information that helps you authenticate yourself in a request that you’re making.
In the following section, you’ll learn how to use the Python requests library to customize HTTP headers being passed into a GET request.
How to Pass HTTP Headers into a Python requests GET Request
To pass HTTP headers into a GET request using the Python requests library, you can use the headers= parameter in the .get() function. The parameter accepts a Python dictionary of key-value pairs, where the key represents the header type and the value is the header value.
Because HTTP headers are case-insensitive, you can pass headers in using any casing. Let’s see how you can pass headers into a requests.get() function:
# Passing HTTP Heades into a Python requests.get() Function import requests url = 'https://httpbin.org/get' headers = print(requests.get(url, headers=headers)) # Returns:
Let’s break down what we did in the code above:
- We declared a url variable, holding the endpoint we want to connect to
- We declared a dictionary, headers , which contained the headers we wanted to pass in
- Finally, we used the requests.get() function to pass in our headers into the headers= parameter.
In the next section, you’ll learn how to pass HTTP headers into a Python requests.post() function.
How to Pass HTTP Headers into a Python requests POST Request
In order to pass HTTP headers into a POST request using the Python requests library, you can use the headers= parameter in the .post() function. The headers= parameter accepts a Python dictionary of key-value pairs, where the key represents the header type and the value is the header value.
Because HTTP headers are case-insensitive, you can pass headers in using any case. Let’s see how you can pass headers into a requests.post() function:
# Passing HTTP Headers into a Python requests.post() Function import requests resp = requests.post( 'https://httpbin.org/post', headers=) print(resp) # Returns:
Let’s break down what we did in the code block above:
- We imported the requests library
- We created a response object using the requests.post() function
- In the function, we passed in a URL. We also passed in a dictionary of headers.
By printing out the response, we were able to see that the response returned a successful 200 response.
How to Check Headers Returned in a Python requests Response Object
In the two previous sections, you learned how to pass headers into both GET and POST requests. However, even when you don’t pass in headers, headers will be returned in a Response object. The headers are accessible via the .headers attribute, which returns a dictionary.
Let’s see how we can return the headers available in a Response object:
# Checking the Headers of a Response Object import requests response = requests.get('https://httpbin.org/get') print(response.headers) # Returns: #
In the code block above, we used a get() function call to get a Response object. We then accessed the headers of the response by accessing the .headers attribute.
You can also access a single header by accessing the key of the dictionary:
# Accessing a Single Header in a Response Object import requests response = requests.get('https://httpbin.org/get') print(response.headers.get('content-type')) # Returns: application/json
There are two main things to note about the code block above:
- We used the .get() method in order to safely return a value. This ensures that, if a key-value pair doesn’t exist, no error is thrown
- We were able to access the value without case sensitivity
Conclusion
In this tutorial, you learned how to use headers in the Python requests library. You first learned what HTTP headers are and what they are used for. Then, you learned how to use these headers in request get() and post() functions. Finally, you learned how to access headers in a Python requests Response object.
Additional Resources
To learn more about related topics, check out the tutorials below: