1

I'm trying to emulate server side form post action at Flask. Throught using Requests lib I doing post action:

@flask_app.route('/hello',methods=['GET','POST'])
def hello():
    r   = requests.post(              
             "https://somesite.hello"
            , data= {'user': 'bill','pass': 123})
    return r.text                             
    #return redirect("https://somesite.hello")

And it's work. But when I posting real form by html it also doing redirection. After #1 url will be http://mysite.hello:5000/hello, but I need contnent from #1 but url from #2 https://somesite.hello How to do 1 and 2 at same time? Thank you!

UPD:

I found some solution, i not really like it, because user will be see "Redirecting to billing page" page for a second. May be someone know how do requests and redirect to get some result?

@flask_app.route('/hello',methods=['GET','POST'])
def hello():
    url = "https://somesite.hello"
    return flask_app.make_response("""
    <html>
        <body onload='document.forms[0].submit()'>
            Redirecting to billing page...
            <form action='%s' method='post'>
                <input type='hidden' name='user' value='%s'>
                <input type='hidden' name='pass' value='%s'>
            </form>
        </body>
    </html>
    """%(url
        ,'bill'
        ,123)) 
Ivan Bryzzhin
  • 2,009
  • 21
  • 27
  • This may help: http://stackoverflow.com/questions/17057191/flask-redirect-while-passing-arguments – azalea Dec 23 '14 at 14:45
  • Then how are you going to use r.text? – azalea Dec 23 '14 at 14:54
  • My flask app is communicating with billing site. To make an order you have to make firts html form with some data (shop_id, pass_id, sign), and when you press submit button on it (this is post action what i try to do by flask) redirect happened to sec form where you can write card information and submit to pay. I try do it with only one step. This r.text is second form on billing site, but windows.location is my site. I don't know how to change windows.location by flask. Only way by jscript? – Ivan Bryzzhin Dec 23 '14 at 15:06

1 Answers1

2

Simply don't follow the redirect and issue a redirect of your own to the following page:

r = requests.post('some.url', data={'some': 'data'}, allow_redirects=False)
return redirect(r.headers['location'])

We submit the form data, but don't follow the redirect with Requests. Instead, we pass the redirect on to the end user. However, this will not work if the payment processor uses cookies to preserve state (which it most likely does) as the cookie that some.url sets for your request cannot be passed on for some.url.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • Thank you for answer, but it`s don`t wonna work with my billing. I'm not sure why exactly. I have found one solution, i will post it here, but it more like trick. – Ivan Bryzzhin Dec 24 '14 at 08:05