Understanding parsers and renderers

The serialized data has to be rendered in a specific format before you return it in an HTTP response. Likewise, when you get an HTTP request, you have to parse the incoming data and de-serialize it before you can operate with it. REST framework includes renderers and parsers to handle that.

Let's see how to parse incoming data. Execute the following code in the Python shell:

>>> from io import BytesIO>>> from rest_framework.parsers import JSONParser>>> data = b'{"id":4,"title":"Programming","slug":"programming"}'>>> JSONParser().parse(BytesIO(data)){'id': 4, 'title': 'Programming', 'slug': 'programming'}

Given a JSON string input, you can use the JSONParser class provided by REST framework to convert it ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.