Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormDataRoutingRedirect exception from a URL without trainling slash

I'm doing an ajax POST request and I'm getting this exception:

[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100]     self.raise_routing_exception(req)
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100]   File "/usr/lib/python2.6/site-packages/flask/app.py", line 1439, in raise_routing_exception
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100]     raise FormDataRoutingRedirect(request)
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100] FormDataRoutingRedirect: A request was sent to this URL (http://example.com/myurl) but a redirect was issued automatically by the routing system to "http://example.com/myurl/".  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.

The route definition is this:

@app.route('/myurl')
def my_func():

I can see in Firebug that the request is sent without the trailing slash:

http://example.com/myurl
Content-Type    application/x-www-form-urlencoded; charset=UTF-8

I have this in another module:

@app.route('/')
@app.route('/<a>/')
@app.route('/<a>/<b>')
def index(a='', b=''):

Could this last one be getting in the way? Or what? Flask version is 0.10.1

like image 719
Clodoaldo Neto Avatar asked Nov 29 '13 21:11

Clodoaldo Neto


1 Answers

My guess is that your /myurl route is not defined to accept POST requests, and your /<a>/ route is, so Werkzeug picks /<a>/.

The behavior for routes that end in a slash is explained here. By default invoking a route defined with a trailing slash without that slash triggers a redirect to the trailing slash version of the URL. This of course does not work well for POST requests, so you get the FormDataRoutingRedirect exception.

I suspect if you send your POST request to /myurl/ then your /<a>/ route will be invoked just fine, though clearly this is not what you want.

What I think you are missing is accepting POST requests on /myurl, which you can do as follows:

@app.route('/myurl', methods = ['GET', 'POST'])
def my_func():
like image 73
Miguel Avatar answered Nov 05 '22 14:11

Miguel