I have a web app, backend using Django, frontend using normal HTML5.
I the frontend, I use axios to send an array of objects via POST request.
axios
({
method: 'POST',
url: test_url,
data: {
[{title:1, isbn:1234},{title:2, isbn:5678}]
}
})
However, in the backend, I could not succeed in parsing the data send from frontend.
def test_url(request):
body = request.body.decode("utf-8")
json_acceptable_string = body.replace("'", "\"")
d = json.loads(json_acceptable_string)
title = d.get('title')
...
I got json decode error in the backend.
How could I easily parse the array of objects sent from frontend in python?
you can try something like this
https://docs.djangoproject.com/en/4.0/ref/request-response/#django.http.QueryDict.getlist
js
axios
({
method: 'POST',
url: test_url,
data: {
"array_list" : [{"title": 1, "isbn": 1234}, {"title": 2, "isbn": 5678}]
}
})
python
def test_url(request):
data = request.POST
array = data.getlist('array_list')