带有重定向的 Werkzeug(Flask)响应不起作用

Werkzeug(Flask) response with redirect not working

我正在构建一个简单的 Flask 应用程序,我想 return 重定向响应。另外,我想保持对 headers.

的完全控制

这是我目前得到的:

from flask import Flask
from werkzeug.wrappers import Response

app = Flask(__name__)

@app.route('/toexample')
def to_example():

    headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate',
    }

    return Response('www.example.com', status_code=302, headers=headers)

if __name__ == '__main__':
    app.run(debug=True)

我收到一个错误:

TypeError: __init__() got an unexpected keyword argument 'status_code'

好的,看起来 status_code__init__() 上不存在,但是正确的做法是什么?

我最终想要的是一个 link 用户会点击但同样,我想保持对 headers(Connection、Cookies、Referer 等的控制)

这里有两件事要用到。首先,对于要在 to_example 调用中使用 redirect 的 re-direct:

@app.route('/toexample')
def to_example():
    return redirect("http://www.example.com", code=302)

现在,为了控制自定义 headers 和 cookie,您可以使用 after_request,这将允许您在向您的网站发出请求后设置某些自定义细节响应 object:

@app.after_request
def after_request(response):
    response.headers.add('Custom-Header', 'Custom Header')
    response.headers.add('Content-Type', 'application/json')
    response.set_cookie('some-cookie', value='some-cookie-value')
    return response

将所有这些放在一起,您的示例现在看起来像这样:

from flask import Flask, redirect

app = Flask(__name__)


@app.route('/toexample')
def to_example():
    return redirect("http://www.example.com", code=302)


@app.after_request
def after_request(response):
    response.headers.add('Custom-Header', 'Custom Header')
    response.headers.add('Content-Type', 'application/json')
    response.set_cookie('some-cookie', value='some-cookie-value')
    return response

if __name__ == '__main__':
    app.run(debug=True)

这是一个 curl 调用。注意 header.

▶ curl -v http://127.0.0.1:5000/toexample
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET /toexample HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.43.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 FOUND
< Content-Type: text/html; charset=utf-8
< Content-Length: 251
< Location: http://www.example.com
< Custom-Header: Custom Header
< Content-Type: application/json
< Set-Cookie: some-cookie=some-cookie-value; Path=/
< Server: Werkzeug/0.10.4 Python/3.5.0
< Date: Sun, 01 Nov 2015 23:11:30 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
* Closing connection 0
<p>You should be redirected automatically to target URL: <a href="http://www.example.com">http://www.example.com</a>.  If not click the link.%

我不得不将 Location 添加到 headers。还有,status_code是错误的,应该是status=302.

工作示例:

from flask import Flask
from werkzeug.wrappers import Response

app = Flask(__name__)

@app.route('/toexample')
def to_example():

    headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate',
            'Location': 'http://www.example.com'
    }

    return Response('http://www.example.com', status=302, headers=headers)

if __name__ == '__main__':
    app.run(debug=True)

这对我有用:

from flask import Flask, redirect

app = Flask(__name__)


@app.route('/toexample')
def to_example():
    response = redirect("http://www.example.com", code=302)
    headers = dict(response.headers)
    headers.update({'X-Custom-Header1': 'value1', 'X-Custom-Header2': 'value2'})
    response.headers = headers
    return response

这适用于我 Flask v0.10.1。干杯!