pythondjangohttpdjango-viewsdjango-rest-framework

How to use a custom http accept header with Django Rest Framework?


Is there any way to allow Django Rest Framework app to accept the custom accept header like "application/vdn.name.v1+json"?

I keep getting a response like this

Could not satisfy the request Accept header.

Solution

  • Try defining a custom renderer and setting the media_type attribute.

    from rest_framework.renderers import JSONRenderer
    
    class MyRenderer(JSONRenderer):
        media_type = 'application/vdn.name.v1+json'
    

    Then enable your renderer (see the docs for more info)

    REST_FRAMEWORK = {
        'DEFAULT_RENDERER_CLASSES': (
            'path.to.MyRenderer',
            'rest_framework.renderers.JSONRenderer',
            'rest_framework.renderers.BrowsableAPIRenderer',
        )
    }