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?
The Raw HTTP action sends a HTTP request to a web server. How the response is treated depends on the method, but in general the status code and the response headers are returned in variables defined as part of the page load options.
Raw enables full control of the response. This can be especially useful when building a REST API call from a Process. Response. Raw responds with a value and optional response headers. The default ContentType is "text/plain; charset=utf-8".
Definition and Usage. The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the response data (content, encoding, status, etc).
The get() method sends a GET request to the specified url.
Since v1.2.3 Requests added the PreparedRequest object. As per the documentation "it contains the exact bytes that will be sent to the server".
One can use this to pretty print a request, like so:
import requests req = requests.Request('POST','http://stackoverflow.com',headers={'X-Custom':'Test'},data='a=1&b=2') prepared = req.prepare() def pretty_print_POST(req): """ At this point it is completely built and ready to be fired; it is "prepared". However pay attention at the formatting used in this function because it is programmed to be pretty printed and may differ from the actual request. """ print('{}\n{}\r\n{}\r\n\r\n{}'.format( '-----------START-----------', req.method + ' ' + req.url, '\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()), req.body, )) pretty_print_POST(prepared)
which produces:
-----------START----------- POST http://stackoverflow.com/ Content-Length: 7 X-Custom: Test a=1&b=2
Then you can send the actual request with this:
s = requests.Session() s.send(prepared)
These links are to the latest documentation available, so they might change in content: Advanced - Prepared requests and API - Lower level classes
import requests response = requests.post('http://httpbin.org/post', data={'key1':'value1'}) print(response.request.url) print(response.request.body) print(response.request.headers)
Response
objects have a .request
property which is the original PreparedRequest
object that was sent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With