Working with wrappers to write API views

Our code in the games_service/games/ file declared a JSONResponse class and two function-based views. These functions returned JSONResponse when it was necessary to return JSON data and a django.Http.Response.HttpResponse instance when the response was just an HTTP status code. Hence, no matter what the accepted content type that's specified in the HTTP request header, the view functions always provide the same content in the response body—JSON.

Run the following two commands to retrieve all the games with different values for the Accept request header: text/html and application/json. The code file for the sample is included in the restful_python_2_06_01 folder, in the Django01/cmd/cmd601.txt ...

Get Hands-On RESTful Python Web Services - Second Edition 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.