Flask - Resolve 308 Redirect
by
Jeremy Canfield |
Updated: February 25 2024
| Flask articles
Let's say your Flask app has the following route.
@app.route("/api/v1")
def apiv1():
return "hello world"
And when requesting /api/v1 (without the trailing slash) a 308 Redirect is being returned. On the other hand, requesting /api/v1/ (with the trailing slash) returns "hello world" in the route.
This happens because Flask uses werkzeug.routing.Rule which sets strict_slashes to True. With this configuration, the trailing slash must be included. If you want to allow requests without the trailing slash, set strict_slashes to False.
@app.route("/api/v1", strict_slashes=False)
def apiv1():
return "hello world"
Did you find this article helpful?
If so, consider buying me a coffee over at