Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the entire HTTP request that's being sent by my Python application?

In my case, I'm using the requests library to call PayPal's API over HTTPS. Unfortunately, I'm getting an error from PayPal, and PayPal support cannot figure out what the error is or what's causing it. They want me to "Please provide the entire request, headers included".

How can I do that?

like image 786
Chris B. Avatar asked May 14 '12 18:05

Chris B.


People also ask

How do I view headers in Python?

To pass HTTP headers into a GET request using the Python requests library, you can use the headers= parameter in the . get() function. The parameter accepts a Python dictionary of key-value pairs, where the key represents the header type and the value is the header value.

Which Python library would you use to serve HTTP requests?

The requests library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.


2 Answers

A simple method: enable logging in recent versions of Requests (1.x and higher.)

Requests uses the http.client and logging module configuration to control logging verbosity, as described here.

Demonstration

Code excerpted from the linked documentation:

import requests import logging  # These two lines enable debugging at httplib level (requests->urllib3->http.client) # You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA. # The only thing missing will be the response.body which is not logged. try:     import http.client as http_client except ImportError:     # Python 2     import httplib as http_client http_client.HTTPConnection.debuglevel = 1  # You must initialize logging, otherwise you'll not see debug output. logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True  requests.get('https://httpbin.org/headers') 

Example Output

$ python requests-logging.py  INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.org send: 'GET /headers HTTP/1.1\r\nHost: httpbin.org\r\nAccept-Encoding: gzip, deflate, compress\r\nAccept: */*\r\nUser-Agent: python-requests/1.2.0 CPython/2.7.3 Linux/3.2.0-48-generic\r\n\r\n' reply: 'HTTP/1.1 200 OK\r\n' header: Content-Type: application/json header: Date: Sat, 29 Jun 2013 11:19:34 GMT header: Server: gunicorn/0.17.4 header: Content-Length: 226 header: Connection: keep-alive DEBUG:requests.packages.urllib3.connectionpool:"GET /headers HTTP/1.1" 200 226 
like image 187
Inactivist Avatar answered Sep 20 '22 14:09

Inactivist


r = requests.get('https://api.github.com', auth=('user', 'pass')) 

r is a response. It has a request attribute which has the information you need.

r.request.allow_redirects  r.request.headers          r.request.register_hook r.request.auth             r.request.hooks            r.request.response r.request.cert             r.request.method           r.request.send r.request.config           r.request.params           r.request.sent r.request.cookies          r.request.path_url         r.request.session r.request.data             r.request.prefetch         r.request.timeout r.request.deregister_hook  r.request.proxies          r.request.url r.request.files            r.request.redirect         r.request.verify 

r.request.headers gives the headers:

{'Accept': '*/*',  'Accept-Encoding': 'identity, deflate, compress, gzip',  'Authorization': u'Basic dXNlcjpwYXNz',  'User-Agent': 'python-requests/0.12.1'} 

Then r.request.data has the body as a mapping. You can convert this with urllib.urlencode if they prefer:

import urllib b = r.request.data encoded_body = urllib.urlencode(b) 

depending on the type of the response the .data-attribute may be missing and a .body-attribute be there instead.

like image 23
Skylar Saveland Avatar answered Sep 18 '22 14:09

Skylar Saveland