How to redirect to an external url with parameters and POST method?

Majid Zandi picture Majid Zandi · Nov 13, 2014 · Viewed 17.9k times · Source

I want save form data before submit to action url in flask

<form action="" method="POST">
    <input type="hidden" name="PAYEE_ACCOUNT" value="U1234567">
    <input type="hidden" name="PAYEE_NAME" value="Name">
    <input type="text"   name="PAYMENT_AMOUNT">
    <input type="submit" name="PAYMENT_METHOD" />
</form>

I can submit this form, but i want save form data before submit

View:

@mod.route('/payment/', methods=['GET', 'POST'])
def payment():
    if request.method != "POST":
        return render_template('form.html')

    form = request.form

    form_data = {'PAYEE_ACCOUNT': form['PAYEE_ACCOUNT'],
                 'PAYEE_NAME': form['PAYEE_Name'],
                 'PAYMENT_AMOUNT' : form['PAYMENT_AMOUNT']
                 }

    # Save Data

    import urllib
    params = urllib.urlencode(form_data)

    url = 'http://www.example.com'
    return redirect(url, params)

Please help me

Answer

hamidfzm picture hamidfzm · Nov 16, 2014

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

@mod.route('/payment/', methods=['GET', 'POST'])
def payment():
    if request.method != "POST":
        return render_template('form.html')

    form = request.form

    form_data = {'PAYEE_ACCOUNT': form['PAYEE_ACCOUNT'],
                 'PAYEE_NAME': form['PAYEE_Name'],
                 'PAYMENT_AMOUNT' : form['PAYMENT_AMOUNT']
                 }

    # Save Data

    url = 'http://www.example.com'
    return redirect(url, code=307)