How to receive POST data in django
Asked Answered
S

3

39

I've been trying to integrate a payment gateway into my site in django. I'm having trouble getting the response data from the payment gateway.

The payment gateway has sample docs for php which looks like this :

$ErrorTx = isset($_POST['Error']) ? $_POST['Error'] : '';               //Error Number
$ErrorResult = isset($_POST['ErrorText']) ? $_POST['ErrorText'] : '';   //Error message
$payID = isset($_POST['paymentid']) ? $_POST['paymentid'] : '';     //Payment Id

In the view for the url that the payment gateway is redirecting to after entering card details etc, I'm checking if it's a GET if request.method == "GET" and then passing the request to a function. When I debug the request, I can see an empty query dict. and if I try something like res = request.GET['paymentid'] I get an error that says there's no key called paymentid.

Am I missing something obvious? I'm still pretty new to django, so I'm sure I'm doing something wrong.

Satiable answered 27/9, 2011 at 17:59 Comment(7)
If it's being POSTed then the method will be "POST".Middlebreaker
yeah, but the if request.method == "GET" returns true def pgreturn_hdfcerror(request): #process hdfc error request getLogger().debug("pg return hdfc error") if request.method == "GET": return handleHdfcResponse(request, 1)Satiable
What the bleep is that supposed to be?Middlebreaker
terribly sorry, still learning how to format stuff here. Newlines are still a mystery to me. Just meant to say request.method is GET.Satiable
You can't receive POST data in a GET.Middlebreaker
The response I'm getting is a GET. There are two separate conversations I'm having with the payment gateway. In the first, I'm doing response = urllib2.urlopen(settings.HDFC_ENDPOINT, request_data) where request_data has the data I am sending to the PG. I read the response with response.read() which works fine. In the second, I'm redirecting the user to the PG url and the PG in turn redirects the user to a url on my website. This is where I'm stuck and not able to read the data. Is there a different way I should be doing this?Satiable
Figured this out. The Payment Gateway is sending the response as a server-server POST but then redirecting to the error url for some reason. Still haven't gotten the whole thing working. :( Thanks for the help, Alasdair.Satiable
Z
47

res = request.GET['paymentid'] will raise a KeyError if paymentid is not in the GET data.

Your sample php code checks to see if paymentid is in the POST data, and sets $payID to '' otherwise:

$payID = isset($_POST['paymentid']) ? $_POST['paymentid'] : ''

The equivalent in python is to use the get() method with a default argument:

payment_id = request.POST.get('payment_id', '')

while debugging, this is what I see in the response.GET: <QueryDict: {}>, request.POST: <QueryDict: {}>

It looks as if the problem is not accessing the POST data, but that there is no POST data. How are you are debugging? Are you using your browser, or is it the payment gateway accessing your page? It would be helpful if you shared your view.

Once you are managing to submit some post data to your page, it shouldn't be too tricky to convert the sample php to python.

Zel answered 27/9, 2011 at 19:41 Comment(3)
I thought there was no post data too, but the payment gateway guys were giving me so much grief about everything being alright on their end that I convinced myself it was something I was doing wrong. I am debugging using the browser. The payment gateway only redirects to my page. Would the payment gateway code have to be different while sending the data to my page for php and django? They say they support php, etc, but not django.Satiable
Are you sure they are posting to the correct url? If, for example, they are posting to /callback (without the trailing slash), the request may be directed to /callback/, and the POST data would be lost.Zel
as of django 1.5, use request.body https://mcmap.net/q/409895/-django-amp-tastypie-request-post-is-emptyReticulum
B
2

for class based views, try this:

class YourApiView(generics.ListAPIView):

    """
    API endpoint 
    """

    def post(self, request, *args, **kwargs):

        print("request data")
        print(request.data)
Brasilein answered 25/1, 2022 at 21:28 Comment(0)
G
1

You should have access to the POST dictionary on the request object.

Grisons answered 27/9, 2011 at 18:4 Comment(4)
It isn't a dictionary per se, but rather a QueryDict, a different type of mapping.Middlebreaker
@Grisons while debugging, this is what I see in the response.GET:<QueryDict: {}>, POST:<QueryDict: {}> What do you mean 'have access'? Is there some other way I should be trying to access the data?Satiable
You should be checking in the request.POST QueryDict, not the response.POST.Grisons
Sorry, that was a typo. I am checking request. It's definitely an empty querydict in both GET and POST. The only thing that has values in it is COOKIES. :SSatiable

© 2022 - 2024 — McMap. All rights reserved.