[Answered ]-Django returns an array instead of a dictionary (rest-api-framework

1๐Ÿ‘

โœ…

What could be the reason?

This is simply how a QuerDict is represented. This is because in a querystring and in a HTTP header, the same key can occur multiple times. It thus maps a key to the value.

If you subscript the item [Django-doc], like request.POST['token'] it will always return the last element, if you use .getlist(โ€ฆ) [Django-doc], it will return a list of all items:

request.POST['token']          # 1234
request.POST.getlist('token')  # ['1234']

Furthermore, as you found out, you can not pass a dictionary as value. If you want to send this, you need to serialize it, for example as a string:

import json

data = {
    'token':1234,
    'data': json.dumps({
        'name':'test',
        'etc':True,
    })
}

then at the receiving end, you can deserialize these:

import json

json.loads(request.POST['data'])

Leave a comment