0

I am completely new to django , you can I just started it today

when I am making a POST request with parameters using postman , I am always getting None for email,password, name and other variables

@csrf_exempt
def signup(request):
    if request.method != 'POST':
        raise Http404
    email = request.POST.get('email')
    password = request.POST.get('password')
    name = request.POST.get('name')
    os = request.POST.get('os')
    device_id = request.POST.get('device_id')
    version = request.POST.get('version')
    device = request.POST.get('device')
    print "email value is = %s", email
    user=AppUser.objects.get_or_create(email=password,password=password)
    user.save()
    return HttpResponse(json.dumps({"result": True}), content_type='application/json')

Please help , Why it is always showing None even though I am passing values from POST request for email and for other parameters

Below is the body request from post man using POST

http://127.0.0.1:8000/v1.0/signup/?email=nagu@nagu.com&password=nagendra&name=nagendra&os=android&device_id=12345678&version=23.0.1&device=samsung

below postman screen shot

enter image description here

12
  • First, it is highly recommended to use Django Forms for getting data directly from request. Second, please share your template which is responsible for posting the data or write the output of printing request.POST
    – kia
    Jan 2, 2016 at 20:09
  • I do not want to use forms , I want to get the data through api using POST request
    – Naga
    Jan 2, 2016 at 20:12
  • Would you display the content of request.POST?
    – kia
    Jan 2, 2016 at 20:18
  • @kia, If I am using GET then I am able to get the values whatever I am passing through url, I am guess I am missing some concept related to request.
    – Naga
    Jan 2, 2016 at 20:25
  • I need the body of your request to say something useful for you :)
    – kia
    Jan 2, 2016 at 20:31

4 Answers 4

5

The parameters you added to the url are GET parameters not POST parameters. POST parameters are in the request body and not visible through urls. Even you specify your request method is POST with your original url, you are not going to send any data.

If you in your commandline do something like:

curl --data "email=nagu@nagu.com&password=nagendra&name=nagendra&os=android&device_id=12345678&version=23.0.1&device=samsung" http://127.0.0.1:8000/v1.0/signup/

It should send POST data to your view.

Take a look at this SO question and answer on how POST requests are delivered.

2
  • 1
    Curl curl.haxx.se is the most basic one. But it's command line tool and not easy to use. If you want to write programs to do it, try python requests: docs.python-requests.org/en/latest. It's the easiest client side program I ever had.
    – Shang Wang
    Jan 2, 2016 at 20:59
  • You can certainly send POST data in Postman; there's a whole box for it. Jan 2, 2016 at 21:28
1

I have tried postman in chrome. Below is the screenshot. Does this work for you?

postman in chrome

4
  • this does not work for me, I have tried same thing and I am getting email can not be null, I do not know whats happening, is it something related to postman? , if I am using command line it is working like charm but using postman it is not working.
    – Naga
    Jan 3, 2016 at 6:30
  • I have tested postman and it works in y case. Maybe it's better if you share screenshot or something helpful for getting close to the problem.
    – kia
    Jan 3, 2016 at 9:22
  • I have added postman screen shot in question , please have a look
    – Naga
    Jan 3, 2016 at 10:53
  • 1. What value is set for Header (1) tab? 2. Check AppUser.objects.get_or_create(email=password,password=password) 3. Share content of request.POST displaying in your debug page (bottom of yellow page)
    – kia
    Jan 3, 2016 at 11:38
0

please make sure your parameter of body in postman is correct, it should be "x-www-form-urlencoded". THKS!

0
0

check your header you probably may have

Content-type set to application/json

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.