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

前端 未结 5 981
暖寄归人
暖寄归人 2020-11-22 13:37

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

5条回答
  •  渐次进展
    2020-11-22 14:15

    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.

提交回复
热议问题