Make a POST request while redirecting in flask

59,218

The redirect function provided in Flask sends a 302 status code to the client by default, and as mentionned on Wikipedia:

Many web browsers implemented this code in a manner that violated this standard, changing the request type of the new request to GET, regardless of the type employed in the original request (e.g. POST). [1] For this reason, HTTP/1.1 (RFC 2616) added the new status codes 303 and 307 to disambiguate between the two behaviours, with 303 mandating the change of request type to GET, and 307 preserving the request type as originally sent.

So, sending a 307 status code instead of 302 should tell the browser to preserve the used HTTP method and thus have the behaviour you're expecting. Your call to redirect would then look like this:

flask.redirect(flask.url_for('operation'), code=307)
Share:
59,218
ln2khanal
Author by

ln2khanal

Updated on April 13, 2020

Comments

  • ln2khanal
    ln2khanal about 4 years

    I am working with flask. I am in a situation where I need to redirect a post request to another url preserving the request method i.e. "POST" method. When I redirected a "GET" request to another url which accepts "GET" request method is fine. Here is sample code with which I am trying the above..

    @app.route('/start',methods=['POST'])
    def start():
        flask.redirect(flask.url_for('operation'))
    
    @app.route('/operation',methods=['POST'])
    def operation():
        return "My Response"
    

    I want to make a "POST" request to "/start" url which internally also makes a "POST" request to "/operation" url.If I modify code as like this,

    @app.route('/operation',methods=['GET'])
    def operation():
        return "My Response"
    

    code works fine for "GET" request. But I want to be able to make POST request too.

  • ln2khanal
    ln2khanal about 11 years
    Thanks for exact suggestion! (bow) still I beg suggestion of a source where I can learn flask in deep. I want to learn such "code" related stuffs. I think those things are core. Thanks one more time :)
  • mdeous
    mdeous about 11 years
    Well, on this specific point, I think no doc could have helped you, though by looking at the redirect function signature, seeing that it sends 302 codes could have put you on the way. Otherwise, regarding Flask's API, there's nothing better than the official documentation. On the other hand, if you want more in-depth examples, you could look at The Flask Mega-Tutorial which show how to perform many real-world tasks.
  • OzzyTheGiant
    OzzyTheGiant about 7 years
    If you're sending data in your post request that also needs to be redirected to that other page, be sure to access it in the new view's function using 'request.form["<property>"]' where property is the name of the data you are sending, such as 'username'.
  • ln2khanal
    ln2khanal over 6 years
    Nice suggestion, thanks! But my code was such that the endpoint(url) accepts only POST requests. That url could be accessed from outside the world as well from internal redirection. Hope you understand what i am trying to say.
  • mdeous
    mdeous over 6 years
    @ln2khanal I don't understand, this would only affect internal redirects, when requested from the outside world, the /operation endpoint would still behave the same. What doesn't work as expected?
  • ln2khanal
    ln2khanal over 6 years
    [@app.route('/start',methods=['POST']) def start(): flask.redirect(flask.url_for('operation'))] first part in the question description! This should clear your confusions.
  • Sonique
    Sonique over 5 years
    I just wonder, why not create two routes, with and without slash. It's should works without tweaks and fixes and behaviour will be more clear.