Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST data to remote server and display result

I would like to submit some post request from my django app to payment service. Is there a simple way to do so from within a view and then display the remote service instead of my own view (something like being redirected to remote page with post data)

Currently I am messing with some weird approach of displaying a hidden form and ovveridng it's javascript submit call to implement some of my own stuff, but I think it is overcomplicated.

I need something like this:

def view(request, **kwargs):
    do_my_own_business()

    post_data = create_post_dada(**kwargs)

    return post_remote_page("https://example.com", post_data)
like image 460
Tomek Jurkiewicz Avatar asked Feb 02 '26 06:02

Tomek Jurkiewicz


1 Answers

You can use python's urllib2 to to make a request to a 3rd party website. This is essentially emunlating how you or I would visit the website via code - here's a good tutorial.

If you want to make life easier, there are some libraries build upon urllib2 which make doing these external request easier - namely requests and mechanize. If you are from a PHP background you can also use pyCurl (Not based on urllib2)

Be sure that the payment service you are using doesn't have it's own API or library that you could use instead, as this would be much easier and safer then the above approach which is essentially screen scraping.

Furthermore, be aware that for every request your user makes to your server, your server needs to make to an other external server which means you could potentially time out etc. introducing another level of complexity in managing the connections.

like image 114
Timmy O'Mahony Avatar answered Feb 03 '26 21:02

Timmy O'Mahony