Redirect to a URL with POST Method

1,181 views
Skip to first unread message

Sepehr Mohamadi

unread,
Jul 4, 2015, 7:45:24 AM7/4/15
to web...@googlegroups.com
Hi Everybody,

I want to redirect navigation to an outside URL with a POST Method.

I should send payment info to a payment gateway through a POST Method. Then payment gateway again callback to web2py with results.

I searched everywhere in the Internet, Web2py Book and this forum, but could not find a clue!

Please guide!
Thanks,
Sepehr

Massimo Di Pierro

unread,
Jul 4, 2015, 10:09:09 AM7/4/15
to web...@googlegroups.com, sepehr....@gmail.com

Anthony

unread,
Jul 4, 2015, 11:26:32 AM7/4/15
to web...@googlegroups.com, sepehr....@gmail.com
You could return a 307 redirect response, which would instruct the browser to re-post the data to the new URL, but the browser will then likely prompt the user to confirm they want to re-post the data. This will likely be confusing to the user, so should probably be avoided.

If you just need the data posted directly to the payment gateway, but you don't need it posted to your own server at all, then all you have to do is change the "action" attribute of the form to point to the appropriate external URL. If it is a web2py form, something like:

form = FORM(..., _action=third_party_url)

If you need to receive the data on your server first and then have it posted to the gateway, you would not use a redirect. Instead, just have the form post to web2py as usual, and in the controller, send the post request to the gateway using urllib2 or the requests library. web2py also includes the fetch() function in gluon.tools, which uses urllib2 -- if your data are in a dictionary, you would do:

from gluon.tools import fetch
result
= fetch(external_url, data)

You would want to check the result to make sure the post was successful.

Anthony

Sepehr Mohamadi

unread,
Jul 5, 2015, 6:06:59 AM7/5/15
to web...@googlegroups.com, sepehr....@gmail.com
Thanks a lot Anothony,

First I should say yes, I need to recieve data first then redirect user to the gateway.

But, I tried your solutions but non of them worked.

Because I need the user to redirect to gateway to enter some additional info on Gateway site.
But in requests module don't redurict the user there, and just Post the data to the gateway.

Let me know if you have any idea.

Thanks

Massimo Di Pierro

unread,
Jul 5, 2015, 6:10:19 AM7/5/15
to web...@googlegroups.com, sepehr....@gmail.com
This cannot be done for security reasons. If this were allowed than one web page could force your browser to post a request for a bank transfer to your bank making a guess that you are logged in with the bank.

You can only do it via ajax as in the PHP assuming the domain is the same.

Massimo

Anthony

unread,
Jul 5, 2015, 10:45:58 AM7/5/15
to web...@googlegroups.com, sepehr....@gmail.com
Does the gateway provide any integration instructions?

Leonel Câmara

unread,
Jul 6, 2015, 10:03:31 AM7/6/15
to web...@googlegroups.com
Does the gateway allow cross domain requests?

Sepehr Mohamadi

unread,
Jul 6, 2015, 11:22:11 AM7/6/15
to web...@googlegroups.com, sepehr....@gmail.com
All payment gateway in Iran, Do the same!!!

Anthony

unread,
Jul 6, 2015, 11:43:39 AM7/6/15
to web...@googlegroups.com, sepehr....@gmail.com
You will need to provide more details about the requirements. What instructions are provided by the gateway provider? You could post the data from your server and then redirect the user to the gateway site, but the redirect won't be via a POST request. We can't help without more details.

Sepehr Mohamadi

unread,
Jul 9, 2015, 8:46:57 AM7/9/15
to web...@googlegroups.com, sepehr....@gmail.com
Thanks Anthony, 

But Moderators don' let me to attache the gateway document to the forum, Maybe I can give the link to it?

By the way, process is like this:

1- Customer is in my site and request for an order.
2- I call for PayRequest Method in Payment gateway(PG) with payment inf and a callback URL in my own site and PG give me a Confirmation Code.
3- I redirect the User to a URL in PG with Post method with Confirmation Code data.
4- User completes payment information in PG site and after that redirects to my site through the callback URL and a post data confirming payment done.

There seems no problem in steps 1,2 and 4 , but I failed to implement step 3 in web2py because there seems no option for that.

Thanks,

Anthony

unread,
Jul 9, 2015, 10:41:33 AM7/9/15
to web...@googlegroups.com, sepehr....@gmail.com
On Thursday, July 9, 2015 at 8:46:57 AM UTC-4, Sepehr Mohamadi wrote:
Thanks Anthony, 

But Moderators don' let me to attache the gateway document to the forum, Maybe I can give the link to it?

By the way, process is like this:

1- Customer is in my site and request for an order.
2- I call for PayRequest Method in Payment gateway(PG) with payment inf and a callback URL in my own site and PG give me a Confirmation Code.
3- I redirect the User to a URL in PG with Post method with Confirmation Code data.

Are you sure the PG instructions tell you to redirect the user via a post request? That seems unlikely given that browsers will not allow this without popping up a warning to the user. Instead, you should probably simply change the form action attribute so the form posts directly to the PG instead of posting to your server. If the posted data includes financial account numbers, you probably don't want it on your server anyway, as that increases your liability. Anyway, it's hard to say what you need to do without knowing the requirements of the PG.

Anthony

Anthony

unread,
Jul 9, 2015, 10:44:57 AM7/9/15
to web...@googlegroups.com, abas...@gmail.com, sepehr....@gmail.com
FYI, your status has been changed so your posts are no longer moderated. You should be able to include attachments to posts.
Reply all
Reply to author
Forward
0 new messages