1

I send a post request with data (using requests.post('url', data=data) ):

data = {
       'token':1234,
       'data':{
             'name':'test',
             'etc':True,
              }
       }

When processing reguest.POST in django i get:

<QueryDict: {'token':[1234], 'data':['name', 'etc']} >

What could be the reason?

3
  • 1
    It always represents this as a list, since the same key can occur multiple times. Commented Feb 22, 2021 at 11:43
  • @willem-van-onsem How to get data values into 'data'? Commented Feb 22, 2021 at 11:46
  • these are not send prooperly, since you can not send a dictionary as value, you will need to (JSON) serialize this, and deserialize this at the receiving end. Commented Feb 22, 2021 at 11:47

1 Answer 1

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'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.