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.