我正在研究 Django 中的 REST API。
这是我的看法:
if request.method == 'GET':
print "In get*****************"
print "Request",request
queryset = Beer.objects.all()
serializer = BeerSerializer(queryset, many=True)
return Response(serializer.data)
elif request.method == 'POST':
print "In get*****************"
print "Request",request.data
serializer = FosterSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
它工作正常
curl -X POST http://192.168.2.241:8080/beers/ \
-d '{"beer_type": "blond", "brand": "Foster" ,"ml": "6000"}' \
-H "Content-Type: application/json"
但不与
curl -X POST http://192.168.2.241:8080/beers/ \
-d '[{"beer_type": "Mild1", "brand": "Foster" ,"ml": "199"},{"beer_type": "Mild", "brand": "Foster" ,"ml": "2"}]' \
-H "Content-Type: application/json"
如何处理 JSON 对象列表?