Changing Content-Type responses in Django Rest Framework?

I need all the error responses in my API to stick to the "Problem Details for HTTP API" Internet project , which in the context of Django Rest Framework could simply mean changing Content-Type JSON responses to "application / problem + json". while successful responses stick to "application / json". What's the best way to accomplish this?

I thought about creating a custom exception handler that would do the following:

  • If "application / problem + json" is not accepted by the client, a "406 Not Acceptable" response is returned.
  • Otherwise, call rest_framework.views: exception_handler () and set the response content type to "application / problem + json". (Let's assume I have a render for this type.)

Is there a better way to do this? If not, can you identify any problems with this approach?

+3


source to share





All Articles