Yuriy
Yuriy

Reputation: 51

How to get data from POST request in Django

I spent a lot of time trying to get data from POST request in my django 1.8 application and it doesn't work whatever I do. Hopping that somebody more skillful suggest the solution.

I need to get data from the request:

curl.exe -X POST --data "Status=OK"  http://localhost:8000/postback

relevant content of urls.py :

url(r'^postback', 'app.views.process_postback'),

relevant content of views.py :

@csrf_exempt
def process_postback(request):
    if request.method == 'POST':
        dict = request.POST
        print(dict)

    return HttpResponse("OK")

I always get empty value in dict : My question how to get Status from post to inside my app

Here is few more details:

  1. I process postbacks from third party website, so I cannot change content of POST request
  2. I don't need(want) any forms/models for the request, I just need to collect value of Status.

Thanks for help.

Upvotes: 3

Views: 2284

Answers (3)

Luffey Luo
Luffey Luo

Reputation: 1

I used django 1.10.so i only can give some advice.when i want to get the value from the form ,no matter GET or POST,i useddict=request.POST.get('dict') The second 'dict' is your form's input name .Such as in html , it's should be `

<form name="input" method="post">

   <input type="text" name="dict" >

   <input type="submit" value="submit">

</form>`

Upvotes: 0

Yuriy
Yuriy

Reputation: 51

I finally figured out what's was wrong. I use VS 2013 with PTVS (Python Tool for Visual Studio) as my development environment. And it appears that if project launched under debugger then http request gets screwed up. If I run project without debugger then everything works fine. I tried to look at the debugger code (C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_debugger.py) but didn't find some obvious things that would cause such behavior. If anybody came across it and knows how to fix it would be great to know.

Upvotes: 2

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

Following the https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

Remove -X POST option from Your command

curl.exe --data "Status=OK"  http://localhost:8000/postback

Output:

<QueryDict: {u'Status': [u'OK']}>
[04/Dec/2015 21:03:15] "POST /postback HTTP/1.1" 200 2

pip freeze output:

Django==1.8.6

Upvotes: 0

Related Questions