Python requests - print entire http request raw?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When HTTP calls fail, seeing the full outgoing request is often faster than guessing from partial logs. In Python requests, the most reliable inspection point is a PreparedRequest. That object contains final method, URL, headers, and body after request construction logic runs.
Prepare and Inspect the Final Request
Use requests.Request plus Session.prepare_request to inspect exactly what will be sent.
This avoids confusion from unprepared request objects that may miss auto-added headers.
Build a Raw-Style HTTP Preview
For debugging, you can format the prepared request into raw-looking text.
This format is useful in ticket attachments and integration tests.
Dump Full Request and Response Together
requests-toolbelt can dump request and response bytes in one step.
Use this in controlled debugging environments because output can be large and sensitive.
Enable Transport-Level Debug Logging
For deeper diagnostics in connection layers, turn on urllib3 and HTTP connection debug logging.
Disable this after troubleshooting to avoid log noise and sensitive data exposure.
Redact Secrets Before Logging
Never print tokens, cookies, or credentials directly. Redact sensitive headers before output.
Apply redaction consistently in all request-dump utilities.
Reusable Debug Wrapper
Keep this utility under source control. In shared SDK code, include an option flag to enable or disable request dumping per call. This prevents globally noisy logs and lets support engineers activate deep diagnostics only for failing endpoints. Structured request dump metadata such as correlation ID and timestamp also makes incident timelines much easier to reconstruct. A wrapper function keeps inspection behavior consistent.
This avoids repeated ad hoc logging code and reduces mistakes.
Testing Outgoing Requests
When requests are signed or modified by middleware, capture request snapshots before and after each processing step. This staged comparison makes it easier to find exactly where headers, query parameters, or body content changed unexpectedly.
In unit tests, you can assert request composition by mocking Session.send and checking prepared fields. This catches errors in headers and payload structure before network calls happen.
For integration tests, compare formatted request snapshots while keeping redacted fields deterministic.
Common Pitfalls
- Logging
Requestinstead ofPreparedRequestand missing final headers. - Printing binary bodies directly without safe decoding.
- Leaving transport debug logging enabled in production.
- Logging credentials and tokens in plain text.
- Assuming body serialization is unchanged after auth middleware layers.
Summary
- Use
PreparedRequestto inspect the true outgoing HTTP request. - Build raw-style previews for readable diagnostics.
- Use toolbelt dumps when full request-response context is needed.
- Redact sensitive headers and payload fields before logging.
- Centralize debug utilities for consistent and safe observability.
Related reading
- Python Requests throwing SSLError
- Python retrieve several URLs via select.epoll
- RabbitMQ - Get messages from a queue using curl
- RabbitMQ C# API Event based Message Consumption
- Python requests - threads/processes vs. IO
- Python re.sub group number after number
- RabbitMQ Declare Exchange from Terminal - Access refused /api/exchanges/
- RabbitMQ failed to start, TCP connection succeeded but Erlang distribution failed

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.