0

My client is passing this json as a post to django server:

data={  'supplier': supplier_name,
        'date': date,
        'payment':payment,
        'materials':[{"name":name,"qtd":qtd,"price":price},
                    {"name":name,"qtd":qtd,"price":price},
                    {"name":name,"qtd":qtd,"price":price}]
}

I'm using push to put materials:

data['materials'].push({"name":name,"qtd":qtd,"price":price});

My django view handles data like this:

supplier=request.POST.get('supplier')
date=request.POST.get('date')

When I try to do this, the materials content is "none":

materials=request.POST.get('materials')

How can get a list use in further code?

Ajax is being sent like this:

$.ajax({
    type:"POST",
    url:"{% url 'validate_purchase' %}",
    data: data,
    dataType: 'json',
    success: function(data){
    }
});
hopieman
  • 399
  • 7
  • 22
  • duplicates https://stackoverflow.com/questions/1208067/wheres-my-json-data-in-my-incoming-django-request?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa see https://stackoverflow.com/a/3244765/6627564 this answer – Alexandr Tatarinov May 09 '18 at 17:09
  • is this exact what your client is passing to django app - `{"name":name,"qtd":qtd,"price":price} {"name":name,"qtd":qtd,"price":price}`? Looks like this is not a valid JSON, you missed `,` between `} {` in your `materials` array. – Chiefir May 09 '18 at 17:12
  • @Chiefir I missed "," because this was just to exemplify, I am using data['materials'].push({"name":name,"qtd":qtd,"price":price}) to put data – hopieman May 09 '18 at 17:22
  • You should show the part of your JS which actually sends the data. Are you sending it as a form-encoded POST with the JSON in the `data` field, or are you sending it as a JSON POST directly? – Daniel Roseman May 09 '18 at 17:29
  • edited, with ajax in jquery – hopieman May 09 '18 at 17:33

2 Answers2

2

If you are passing data using Content-Type: application/json, You can access json from request.body

Example:

(myblog)  ✘ ✝ ~/projects/myblog/base  up-sell±  curl --header "Content-Type: application/json" \
--request POST \
--data '{"supplier": "x", "date": "x", "materials": [{"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}], "payment": "x"}' \
http://localhost:8000/motor/upsell/set-upsell/ \
> -H "Content-Type: application/json"

views.py:

ipdb> import json
ipdb> json.loads(request.body)
{u'supplier': u'x', u'date': u'x', u'materials': [{u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}], u'payment': u'x'}

Update:

Example by ajax call

Here is ajax function,

data = {"supplier": "x", "date": "x", "materials": [{"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}], "payment": "x"}

$.ajax({
    type: 'POST',
    url: 'http://localhost:8000/motor/upsell/set-upsell/',
    data: JSON.stringify(data),
    contentType: "application/json",
    dataType: 'json'
});

Python code,

ipdb> import json
ipdb> request.body
'{"supplier":"x","date":"x","materials":[{"price":"x","qtd":"x","name":"x"},{"price":"x","qtd":"x","name":"x"},{"price":"x","qtd":"x","name":"x"}],"payment":"x"}'
ipdb> json.loads(request.body)
{u'supplier': u'x', u'date': u'x', u'materials': [{u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}], u'payment': u'x'}
ipdb>
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
  • Is giving me this error(raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)) – hopieman May 09 '18 at 18:20
2

Data to be sent as json must be "stringified", so you need to do "JSON.stringify(data)"

$.ajax({
        type:"POST",
        url:"{% url 'validate_purchase' %}",
        data: JSON.stringify(data),
        dataType: "application/json; charset=UTF-8",
        success: function(data){
        }
    });
hopieman
  • 399
  • 7
  • 22