Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests - print entire http request (raw)?

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?

like image 630
huggie Avatar asked Dec 18 '13 12:12

huggie


People also ask

What is a raw 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.

What is raw response?

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".

What is HTTP request in Python?

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).

What does requests get () do?

The get() method sends a GET request to the specified url.


2 Answers

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

like image 71
AntonioHerraizS Avatar answered Sep 23 '22 23:09

AntonioHerraizS


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.

like image 41
Payman Avatar answered Sep 24 '22 23:09

Payman