简体   繁体   English

使用POST请求发送上下文数据

[英]Sending context data with POST request

I am trying to add a payment gateway to my website. 我正在尝试将支付网关添加到我的网站。 I have a 'Pay now' button has 'href' attribute to the view which handles the payment. 我有一个“立即付款”按钮,该视图的“ href”属性用于处理付款。 This view provides all the varuables needed by the gateway to process the request. 该视图提供了网关处理请求所需的所有变量。 In the end of the view, I redirect the user to the payment gateway page with the variables being passed in the RequestContext . 在视图的最后,我将用户重定向到付款网关页面,并在RequestContext中传递变量。 How do I send them as a form fiels to the payment gateway site? 如何将它们以表格形式发送到付款网关站点?

Here is my view to handle the checkout: 这是我处理结帐的观点:

import hashlib

def redirect(request):
    basket = request.basket
    key = 'to be provided by payu'
    txnid = basket.id,
    amount = basket.total_incl_tax
    phash = hashlib.sha(str(key)+'|'+str(txnid))
    variables = RequestContext(request, {
        'hash' : phash.hexdigest(),
        'key' : key,
        'txnid': txnid,
        'amount' : amount,
    })
    return HttpResponseRedirect('https://test.payu.in/_payment', variables)

So, basically my question is, is it possible to send the above variables in a POST request to the url https://test.payu.in/_payment ? 所以,基本上我的问题是,是否可以在POST请求中将上述variables发送到URL https://test.payu.in/_payment If not, then what are the alternatives? 如果没有,那有什么替代方案?

First of all, dont name your function redirect it is already taken. 首先,不要命名您的函数重定向,它已经被使用。 So here i am using django's redirect: 所以在这里我正在使用Django的重定向:

One way would be to pass variables in url. 一种方法是在url中传递变量。 Here just a pseudo example... 这里只是一个伪示例...

return redirect('https://test.payu.in/_payment?k=%s&h=%s', % ( key, hash)) 

If you faced such problem there's slight chance that you had over-complicated your design. 如果您遇到此类问题,则您的设计极有可能过于复杂。

This is a restriction of HTTP that POST data cannot go with redirects. 这是HTTP的限制,POST数据不能与重定向一起使用。

If you really need to redirect in a POST request try it in client side using JavaScript. 如果您确实需要重定向POST请求,请尝试使用JavaScript在客户端进行重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM