Reputation: 10454
I'm using django 1.8 rest api as below.
@api_view(['GET', 'POST'])
@authentication_classes((TokenAuthentication, SessionAuthentication, BasicAuthentication))
@permission_classes((IsAuthenticated,))
@staff_member_required
def api(request):
print request.data
The problem with this is that it retrieves all parameters as string so I have to convert numbers and boolean manually.
I tried using:
print json.loads(request.body)
but apparently django rest framework reads from the data stream which causes this error:
Error: RawPostDataException: You cannot access body after reading from request's data stream
I also tried
json.loads(request.stream.body)
because the documentation says stream should work.
Is there a way I can retrieve the request post data with the appropriate types?
Note that I'm using ajax to send the data with JSON.stringify.
Upvotes: 1
Views: 338
Reputation: 13733
You want to use serializers
in DRF. They will allow you to convert model instances to native Python datatypes which can be translated into JSON and vice versa. You can also deserialize which will convert those JSON strings to complex types.
Read more about serializers here
Upvotes: 2