How do I disable Flask 308 redirect for trailing slash?

Viewed 364

I have such endpoints

@app.route('/values', methods=['GET', 'POST'])
def operation_types():
    return '', 200


@app.route('/values/', methods=['GET', 'DELETE', 'PUT'])
@app.route('/values/<code>', methods=['GET', 'DELETE', 'PUT'])
def operation_type_(code: str = ''):
    if request.method == 'GET':
        return '', 200
    if request.method == 'DELETE':
        return '', 200
    if request.method == 'PUT':
        return '', 201
    return '', 405

I want the app to behave like this

GET /values         # lists all values
POST /values        # adds a new value

GET /values/123     # gets a value
DELETE /values/123  # deletes a value
PUT /values/123     # updates a value

and I also want this, to be treated as the later:

GET /values/        # gets a value with code ""
DELETE /values/     # deletes a value with code ""
PUT /values/        # updates a value with code ""

In any other case I want to get 404 if the route is missing, or 405 if method is not allowed.

But here's my problem:

DELETE  /values  # this should return 405, because method isn't allowed for /values, only for /values/

But instead I get 308, with Location: header redirecting me to /values/ and also flask renderes wierd HTML body from Werkzeug.

I want to disable this automatic redirect completely, I don't want flask to do any kind of redirects for me.

PS: I tried setting strict_slashes, but then GET /values/ is being mistaken for GET /values. PS: I know I can add DELETE to the first endpoint methods, but that only works for a single endpoint; and I want to set it in the whole application.

0 Answers
Related