Skip to content

Python Requests: Session & Response Objects Explained

Mahesh Shukla - Aka JailBreaker 🚀 edited this page Jun 15, 2024 · 1 revision

Sessions and Request/Response Objects in Requests Library

Table of Content

  1. Session Objects
  2. Purpose of Session Objects
  3. Session Methods
  4. Response Objects
  5. Providing Default Data to Requests
  6. Overriding Session Parameters
  7. Using Sessions as Context Managers
  8. Accessing Response Headers

Session Objects

import requests

# Creating a Session object
s = requests.Session()

# Making GET requests with the Session object
s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('https://httpbin.org/cookies')
print(r.text)  # Outputs '{"cookies": {"sessioncookie": "123456789"}}'

Purpose of Session Objects

Session objects in the Requests library serve multiple purposes:

  • Persist parameters across multiple requests: They allow you to maintain certain parameters, such as headers and authentication credentials, across multiple HTTP requests.
  • Persist cookies across requests made from the Session instance: When you use a Session object, cookies received in responses are automatically stored and sent with subsequent requests to the same domain.
  • Utilize urllib3's connection pooling for performance improvement via HTTP persistent connection: The underlying HTTP connection is reused for multiple requests to the same host, leading to improved performance, especially when making several requests in a short period.

Session Methods

Session objects have all the methods of the main Requests API. This means you can use methods like get(), post(), put(), delete(), etc., just like with regular requests.

Example: Persisting Cookies

The following example demonstrates how Session objects persist cookies across requests:

import requests

s = requests.Session()
s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('https://httpbin.org/cookies')
print(r.text)  # '{"cookies": {"sessioncookie": "123456789"}}'
  • In this code, the Session object s is used to send two HTTP requests. The first request sets a cookie, and the second request retrieves all cookies set by the server, including the one set in the first request.

Response Objects

  • Response objects contain all the information returned by a server after making a request. Here's an example of accessing and using a Response object
# Making a simple GET request
r = requests.get('https://en.wikipedia.org/wiki/Monty_Python')

# Accessing response headers
print(r.headers)  # Outputs the server's response headers

# Accessing request headers
print(r.request.headers)  # Outputs the headers sent in the request

Providing Default Data to Requests

You can provide default data, such as authentication credentials or custom headers, to all requests made using a Session object. Here's an example:

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# both 'x-test' and 'x-test2' are sent
s.get('https://httpbin.org/headers', headers={'x-test2': 'true'})
  • In this code, the Session object s is configured with default authentication (auth) and headers (headers). When making a request, these default settings are used unless overridden in the request itself.

Overriding Session Parameters

You can override session-level parameters for specific requests. Here's an example

s = requests.Session()

r = s.get('https://httpbin.org/cookies', cookies={'from-my': 'browser'})
print(r.text)  # '{"cookies": {"from-my": "browser"}}'

r = s.get('https://httpbin.org/cookies')
print(r.text)  # '{"cookies": {}}'

In this code, the first request to https://httpbin.org/cookies includes a custom cookie, while the second request does not, as it doesn't specify any cookies.

Manually Adding Cookies

  • You can manipulate cookies in a Session object using the Cookie utility functions. For more complex cookie handling, refer to the Requests documentation.

Using Sessions as Context Managers

  • Sessions can be used as context managers, ensuring proper cleanup of resources. Here's an example
with requests.Session() as s:
    s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')

In this code, the Session object s is used within a with block, ensuring that the session is closed properly after the block is executed.

Removing a Value from a Dict Parameter

  • To omit session-level keys from a dictionary parameter in a request, set the key's value to None in the method-level parameter.

Accessing Session Values

  • All values contained within a session, such as cookies, headers, and authentication data, are directly accessible through the Session object.

Request and Response Objects

  • Request Objects
  • A Request object is created when making an HTTP request using methods like requests.get(), requests.post(), etc. It contains information about the request being sent to the server.

Response Objects

  • A Response object is generated when the server responds to a request. It contains all the information returned by the server, including headers, status code, and response content.

Example: Simple GET Request

r = requests.get('https://en.wikipedia.org/wiki/Monty_Python')

In this example, r is a Response object containing the server's response to the GET request made to the specified URL.

Accessing Response Headers

  • You can access the headers sent by the server in the Response object. For example
print(r.headers)
# {'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache': 'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', ...}

The headers attribute of the Response object contains a dictionary of response headers.

Additional Resources

For more detailed information and advanced usage of Session And Response Objects in the Requests library, refer to the official Session API Documentation.

By understanding and effectively utilizing Sessions along with Request and Response objects, you can enhance the efficiency, performance, and versatility of your HTTP requests and interactions in Python.

Happy coding with Requests!