Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask POSTs with Trailing Slash

The documentation states that the preferred way to define a route is to include a trailing slash:

@app.route('/foo/', methods=['GET'])
def get_foo():
    pass

This way, a client can GET /foo or GET /foo/ and receive the same result.

However, POSTed methods do not have the same behavior.

from flask import Flask
app = Flask(__name__)

@app.route('/foo/', methods=['POST'])
def post_foo():
    return "bar"

app.run(port=5000)

Here, if you POST /foo, it will fail with method not allowed if you are not running in debug mode, or it will fail with the following notice if you are in debug mode:

A request was sent to this URL (http://localhost:5000/foo) but a redirect was issued automatically by the routing system to "http://localhost:5000/foo/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction


Moreover, it appears that you cannot even do this:

@app.route('/foo', methods=['POST'])
@app.route('/foo/', methods=['POST'])
def post_foo():
    return "bar"

Or this:

@app.route('/foo', methods=['POST'])
def post_foo_no_slash():
    return redirect(url_for('post_foo'), code=302)

@app.route('/foo/', methods=['POST'])
def post_foo():
    return "bar"

Is there any way to get POST to work on both non-trailing and trailing slashes?

like image 689
Matthew Moisen Avatar asked Mar 08 '17 01:03

Matthew Moisen


People also ask

How to detect a trailing / in a flask route?

You are on the right tracking with using strict_slashes, which you can configure on the Flask app itself. This will set the strict_slashes flag to False for every route that is created app = Flask ('my_app') app.url_map.strict_slashes = False Then you can use before_request to detect the trailing / for a redirect.

How can I redirect to the URL without the trailing slash?

From my understanding, strict_slashes=Falsecan be set on the route to get effectively the same behavior of case two in case one, but what I'd like to do is get the redirect behavior to always redirect to the URL withoutthe trailing slash. One solution I've thought of using would be using an error handler for 404's, something like this.

How to give feedback to a user with the flashing system?

Flask provides a really simple way to give feedback to a user with the flashing system. The flashing system basically makes it possible to record a message at the end of a request and access it on the next (and only the next) request. This is usually combined with a layout template to expose the message.


1 Answers

Please refer to this post: Trailing slash triggers 404 in Flask path rule

You can disable strict slashes to support your needs

Globally:

app = Flask(__name__)
app.url_map.strict_slashes = False

... or per route

@app.route('/foo', methods=['POST'], strict_slashes=False)
def foo():
    return 'foo'

You can also check this link. There is separate discussion on github on this one. https://github.com/pallets/flask/issues/1783

like image 132
Wojciech Jakubas Avatar answered Oct 08 '22 19:10

Wojciech Jakubas