在我的例子中,我使用requests
库通过HTTPS调用PayPal的API.不幸的是,我从PayPal收到了一个错误,而PayPal的支持人员无法找出错误是什么,或者是什么导致的.他们希望我"请提供整个请求,包括标题".
我该怎么做?
在我的例子中,我使用requests
库通过HTTPS调用PayPal的API.不幸的是,我从PayPal收到了一个错误,而PayPal的支持人员无法找出错误是什么,或者是什么导致的.他们希望我"请提供整个请求,包括标题".
我该怎么做?
一种简单的方法:在请求的最新版本(1.x和更高版本)中启用日志(log)记录.
请求使用http.client
和logging
模块配置来控制日志(log)记录详细程度,如here所述.
从链接文档中摘录的代码:
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')
$ 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