如何使用Flask返回相对URI位置标题? [英] How to return a relative URI Location header with Flask?

查看:600
本文介绍了如何使用Flask返回相对URI位置标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建我的HTTP响应时,Flask会替换我的位置标头的内容。
它改变了我的实际的相对URI位置标题为绝对的。

  @ app.route('/ votes' (),201,{'location':'/ votes / 1'} 



$ b 我的测试:

$ p $ def test_vote_creation自我):
response = self.app.post('/ votes',
data = json.dumps({
'name':'Test vote'
}),打印(response.headers ['location'])

return http:// localhost / votes / 1 而不是 / votes / 1



如何使用Flask jsonify返回相对URI位置标题?
$ b 编辑:根据当前版本HTTP / 1.1标准RFC 2616中,Location头的值必须是绝对URI
但是RCF也将改变为允许相对URI。所以我想改变我的API的默认行为,以在我的位置标头中的相对URI来回答。



更多这篇文章

解决方案

HTTP RFC 指定 Location 必须是一个绝对URI:


< h3> 14.30位置

位置响应头字段用于将接收方
重定向到Request-URI以外的位置,以完成请求
或识别新资源。对于201(创建)响应,
位置是由请求创建的新资源的位置。
对于3xx响应,位置应该指出服务器首选的
URI用于自动重定向到资源。
由一个绝对URI组成。 b


因此,Flask / Werkzeug响应对象将任何相对URL 位置标题为一个绝对URL。



您可以覆盖此行为,但我不建议你这样做。要覆盖它,请设置 autocorrect_location_header 响应对象的c $ c>属性到 False jsonify()返回一个响应对象,修改:

  @app .Route('/ votes',methods = ['POST'])
def votes():
response = jsonify()
response.status_code = 201
response.headers ['location'] ='/ votes / 1'
response.autocorrect_location_header = False
return response

但请注意,即使Flask没有,您的WSGI服务器仍然可以执行绝对URL。


Flask replaces the content of my Location header when building my HTTP response. It change my actual relative URI Location header with an absolute one.

@app.route('/votes', methods=['POST'])
def votes():
    return jsonify(), 201, {'location': '/votes/1'}

my test :

def test_vote_creation(self):
    response = self.app.post('/votes',
                             data=json.dumps({
                                 'name': 'Test vote'
                             }), content_type='application/json')
    print(response.headers['location'])

return http://localhost/votes/1 instead of /votes/1

How to return a relative URI Location header with Flask jsonify ?

Edit: According to the current version of the HTTP/1.1 standard, RFC 2616, the value of the Location header must be an absolute URI. But the RCF is going to change to allow relative URIs as well. So I want to change the default behaviour of my API to answer with a relative URI in my location header.

more detail on this post

解决方案

The HTTP RFC specifies that the Location header must be an absolute URI:

14.30 Location

The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.

Location       = "Location" ":" absoluteURI

As such the Flask / Werkzeug response object converts any relative URL Location header to an an absolute URL.

You can override this behaviour, although I would not recommend you do so. To override it, set the autocorrect_location_header attribute of a Response object to False. jsonify() returns a response object, alter that:

@app.route('/votes', methods=['POST'])
def votes():
    response = jsonify()
    response.status_code = 201
    response.headers['location'] = '/votes/1'
    response.autocorrect_location_header = False
    return response

but note that your WSGI server can still enforce an absolute URL even if Flask doesn't.

这篇关于如何使用Flask返回相对URI位置标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆