1

I have a python dict containing a list, for example:

d = {"x": 1, "y": ["a", "b", "c"]}

I want to send this dict with a POST from my python project using the requests lib like this:

requests.post(url, d, verify=False)

I manage to send and recieve the request fine but the problem is that when I'm printing the request.POST dict in my Django view it looks like this:

{"y": "c", "x": "1"}

The y key containing the list doesn't look as expected as the list has been reduced to just the last object of the list, in this case "c". How do I send the list properly?

1
  • I meant to say list, changed all arrays in my question. Commented Jan 6, 2014 at 16:16

1 Answer 1

1

You need to use the QueryDict.getlist() method to retrieve all values for a parameter that is repeated in the request:

y = request.POST.getlist('y')

This assumes that you instead posted d = [('x', 1), ('y', 'a'), ('y', 'b'), ('y', 'c')]; nested lists are not supported by requests.

Sign up to request clarification or add additional context in comments.

1 Comment

Right, thanks, I do use the getlist method but are confusing myself by printing the whole request.POST. The y actually contains an array and not just the "c".

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.