当我在虚张声势(通过 request.session())中为 Web 服务编写处理程序时,我遇到了这个特殊性:
POST 请求在被重定向时会变成 GET 请求。导致我的 POST 被破坏,我无法让它工作。
编辑:实际的请求调用来自虚张声势,如下所述: 如何在 Bravado 中设置自定义 http 客户端?我需要设置额外的客户端证书。
因此,请求呼叫/会话本身被虚张声势所掩盖。
这个问题(请求的 post/get 转换)已经在其他线程中讨论过,根据规范考虑它,但仍然很奇怪。
最后,我只是侵入了 session.py 中以下部分的请求:
def rebuild_method(self, prepared_request, response):
"""When being redirected we may want to change the method of the request
based on certain specs or browser behavior.
"""
method = prepared_request.method
# http://tools.ietf.org/html/rfc7231#section-6.4.4
if response.status_code == codes.see_other and method != 'HEAD':
method = 'GET'
# Do what the browsers do, despite standards...
# First, turn 302s into GETs.
if response.status_code == codes.found and method != 'HEAD':
if method == 'POST':
#print ( '\n\nDeliberately not changing to GET\n\n' )
else:
method = 'GET'
# Second, if a POST is responded to with a 301, turn it into a GET.
# This bizarre behaviour is explained in Issue 1704.
if response.status_code == codes.moved and method == 'POST':
method = 'GET'
prepared_request.method = method
我根本没有改变得到的地方。
还有这里:
def resolve_redirects(self, resp, req, stream=False, timeout=None,
verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs):
我注释掉的地方:
## # https://github.com/requests/requests/issues/1084
## if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect):
## # https://github.com/requests/requests/issues/3490
## print(prepared_request.headers)
## purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding')
## for header in purged_headers:
## prepared_request.headers.pop(header, None)
## prepared_request.body = None
当然这是丑陋的黑客,但我应该怎么做?希望有人对此有指导。