-
Notifications
You must be signed in to change notification settings - Fork 0
/
verbose-requests.py
35 lines (29 loc) · 1005 Bytes
/
verbose-requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
def pretty_print_request(request):
print('{}\n{}\n{}\n\n{}'.format(
'-----------REQUEST-----------',
request.method + ' ' + request.url,
'\n'.join('{}: {}'.format(k, v) for k, v in request.headers.items()),
request.body,
))
def pretty_print_response(response):
print('{}\n{} {}\n{}\n\n{}'.format(
'-----------RESPONSE-----------',
response.status_code,
response.reason,
'\n'.join('{}: {}'.format(k, v) for k, v in response.headers.items()),
response.text,
))
def getVerbose(s, *args, **kwargs):
"""
This acts like requests.Request, but takes session instance as
first parameter and prints request/response representations.
"""
req = requests.Request(*args, **kwargs)
prepared_request = req.prepare()
pretty_print_request(prepared_request)
response = s.send(prepared_request)
pretty_print_response(response)
return response
s = requests.Session()
getVerbose(s,'GET','http://example.com',headers={'X-Whatever':'example header'},data='foo=bar&baz=spam')