Skip to content
Advertisement

POST object via Form-Data ( in Django)

Trying to post the data via multipart (form data) in django backend from react js.

let form_data = new FormData();
let doc = [{ "form" : 1, "city": "Bangalore"}, { "form" : 2, "city": "Delhi"}]

form_data.append("CRegNo", "Nectar00001");
form_data.append("CName", "Nectar");
form_data.append("cityName", doc);
form_data.append("userID", 1);

axios.post("http://127.0.0.1:8000/api/table/", form_data, head)

but in Django it interprets the cityName like this [‘[object Object]’]

Am I doing something wrong ?

Advertisement

Answer

You probably should use JSON.stringify on doc as follows

form_data.append("cityName", JSON.stringify(doc));

Afterwards in your django view you need to parse the data

import json
...

city_name = json.loads(request.POST.get('cityName'))

example using class based views

import json
from django.views import View


class MyView(View):
    def post(self, request):
        city_name = json.loads(request.POST.get('cityName'))
        ....
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement