如何使用Flask返回相对URI位置标题? [英] How to return a relative URI Location header with Flask?
问题描述
它改变了我的实际的相对URI位置标题为绝对的。
@ app.route('/ votes' (),201,{'location':'/ votes / 1'}
$ c $
$ 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屋!