【发布时间】:2023-03-29 09:32:01
【问题描述】:
我必须在 Python 中发出 HTTPS 请求,并且我正在使用 requests 模块来尝试让我的生活更轻松。
请求需要有一个标头和 3 个 FORM 参数 URL 编码。这就是我正在做的事情:
header = {'Content-type': 'application/x-www-form-urlencoded', 'Authorization':'Basic ' + encoded_string, 'Connection': 'Keep-Alive', 'Host':'host.host.com'}
payload='grant_type=authorization_code&code=' + request.args['code'] + '&state=' + request.args['state'] + '&redirect_uri=http://xxx.xyz.com/request_listener'
url = 'https://serviceprovider.xxx.com/auth/j_oauth_resolve_access_code'
response = requests.post(url, data=payload, headers=header, verify=False)
当我尝试返回response 的content 或text 时,我得到一个空字符串。但是,当我打印实际的 response 对象时,它说它是 <Response [200]>,但如果这实际上是 200 OK,那么我发布的服务器也应该转到我指定的 redirect_uri,我会收到通知那里。
这没有发生,我不知道为什么。
【问题讨论】:
-
一些尝试:使用
curl手动构造请求,以确保您知道有效请求的内容应该是什么。如果这可行,那么您就知道您在 Python 中构建或交付请求的方式存在问题。将payload构造为字典而不是字符串。尝试构建一个准备好的请求,以便在将请求发送到服务器之前检查请求状态。
标签: python https python-requests