Reputation: 947
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 ?
Upvotes: 0
Views: 788
Reputation: 600041
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)
.
Upvotes: 1