对POST的请求/虚张声势302响应变为GET(python)

时间:2017-11-24 22:08:31

标签: python python-requests bravado

当我在bravado(通过request.session())编写web服务的处理程序时,我遇到了这个特点:

POST请求在重定向时变为GET请求。 导致我的POST被破坏,我无法使用它。

编辑:实际的请求调用来自bravado,如下所述: {{3}}我需要设置其他客户端证书。

因此请求调用/会话本身被虚张声势掩盖了。

这个问题(按请求进行的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

当然这是丑陋的黑客,但我该怎么做? 希望任何人都有指针。

1 个答案:

答案 0 :(得分:0)

如果您不想更改请求库,可以在代码中检查重定向,这是一个示例:

{{1}}

当然,您必须处理无限循环可能性和其他http状态代码