0

We are trying to make a post request to django api using requests package in python.

Request:

d = {"key1":"123", "key2":[{"a":"b"},{"c":"d"}]}

response = requests.post("http://127.0.0.1:8000/api/postapi/",data=d)

At the server end we are trying to get the parameters using the below code.

def handle_post(request):
    if request.method == 'POST':
        key1 = request.POST.get('key1', '')
        key2 = request.POST.get('key2', '')
        print key1,type(key1)
        print key2,type(key2)
        return JsonResponse({'result': 'success'}, status=200)

I am trying to get the values in key1 and key2.

Expected output:

123,<type 'unicode'>
[{"a":"b"},{"c":"d"}], <type 'list'>

Actual output:

123 <type 'unicode'>
c <type 'unicode'>

How can we get the expected output in django ?

1 Answer 1

1

Use getlist for key2.

key2 = request.POST.getlist('key2', '')

But you might find it easier to send the data as JSON and access json.loads(request.body).

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

1 Comment

using getlist in key2 gave [u'a', u'c'] <type 'list'> rather than the entire dict

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.