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

Naga
  • 1,931
  • 4
  • 25
  • 42
  • 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 02 '16 at 20:09
  • I do not want to use forms , I want to get the data through api using POST request – Naga Jan 02 '16 at 20:12
  • Would you display the content of request.POST? – kia Jan 02 '16 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 02 '16 at 20:25
  • I need the body of your request to say something useful for you :) – kia Jan 02 '16 at 20:31
  • I made the below request from postman using POST method 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 – Naga Jan 02 '16 at 20:35
  • I have edited the question ,and added body of request please have a look – Naga Jan 02 '16 at 20:35
  • Dude, you have shared you localhost URL which is not accessible out of your machine. But I guess you are passing your data via query string which is accessible via request.GET – kia Jan 02 '16 at 20:40
  • https://docs.djangoproject.com/en/1.9/topics/forms/#get-and-post – kia Jan 02 '16 at 20:40
  • yeah it os on my local machine only , I have given it to get some idea . Believe me I am making POST request , If will make GET the it will through 404 as you can see the condition on top in view – Naga Jan 02 '16 at 20:43
  • Dude, I believe you! I say you are making post request, but you are not passing data via post. `email=nagu@nagu.com&password=nagendra&name=nagendra&os=android&device_id=12345678&version=23.0.1&device=samsung` says that you are passing data via query string which is accessible via request.GET. You need for example html form to post them by request. – kia Jan 02 '16 at 20:48
  • I understand, please see postman in chrome app, in which I can select POST and once I add parameters it displays like GET request so that people can what request they are making, may be I wrong but please have a look at postman chrome app , thanks for your time sir – Naga Jan 02 '16 at 20:53

4 Answers4

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.

Community
  • 1
  • 1
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • 1
    `Curl` http://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: http://docs.python-requests.org/en/latest/. It's the easiest client side program I ever had. – Shang Wang Jan 02 '16 at 20:59
  • You can certainly send POST data in Postman; there's a whole box for it. – Daniel Roseman Jan 02 '16 at 21:28
1

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

postman in chrome

kia
  • 828
  • 5
  • 12
  • 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 03 '16 at 06: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 03 '16 at 09:22
  • I have added postman screen shot in question , please have a look – Naga Jan 03 '16 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 03 '16 at 11:38
0

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

0

check your header you probably may have

Content-type set to application/json

Vamen95
  • 3
  • 3