Python requests - print entire http request (raw)?

ghz 3months ago ⋅ 103 views

While using the requests module, is there any way to print the raw HTTP request?

I don't want just the headers, I want the request line, headers, and content printout. Is it possible to see what ultimately is constructed from HTTP request?

Answers

Yes, it is possible to print the raw HTTP request (including the request line, headers, and content) when using the requests module. While requests doesn't natively provide a built-in way to directly output the raw request, you can use the requests-toolbelt library or manually inspect and construct the raw HTTP request from the PreparedRequest object.

Option 1: Using requests-toolbelt

The requests-toolbelt library provides a Dump utility that can dump both the raw request and response as a string. You can install the requests-toolbelt library and use it to see the raw HTTP request.

To install the library:

pip install requests-toolbelt

Then use the Dump utility:

import requests
from requests_toolbelt.utils import dump

# Example request
url = "https://httpbin.org/post"
data = {"key": "value"}

response = requests.post(url, data=data)

# Dump the entire request as a string
request_data = dump.dump_all(response)

# Print the raw request
print(request_data.decode('utf-8'))

This will print the entire request (and response), including the request line, headers, and content.

Option 2: Using PreparedRequest to Print Raw Request

You can manually prepare the request and print the raw HTTP request from the PreparedRequest object in the requests module:

import requests

# Create a session
session = requests.Session()

# Create a Request object
req = requests.Request('POST', 'https://httpbin.org/post', data={'key': 'value'})

# Prepare the request (this step constructs the final raw request)
prepared = session.prepare_request(req)

# Print the raw request
print(f'{prepared.method} {prepared.url} HTTP/1.1')
for header, value in prepared.headers.items():
    print(f'{header}: {value}')
print(f'\n{prepared.body}')

In this example:

  • The Request object is used to create a request.
  • The request is prepared using session.prepare_request() to construct the full request.
  • The method, URL, headers, and body of the PreparedRequest are printed.

This will give you the raw HTTP request, including the request line, headers, and content.

Summary

  • Option 1: requests-toolbelt is the easiest way to print the raw request with its dump utility.
  • Option 2: Manually prepare the request using requests.Session().prepare_request() and print the relevant fields (method, url, headers, and body) to construct the raw HTTP request.