How to customize DRF validation responses

Hey how can i customize response of validation in DRF response

DRF gives below response on invalid JWT Token

{
    "detail": "Given token not valid for any token type",
    "code": "token_not_valid",
    "messages": [
        {
            "token_class": "AccessToken",
            "token_type": "access",
            "message": "Token is invalid or expired"
        }
    ]
}

But i wanna customize it in my way like

'status': False,
'responseCode': 0,
'message': 'Customized error messages ',
'data': {}

I hope a good soul will help me…

You can do so by writing your own exception handler: Exceptions - Django REST framework. I would personally recommend just using an existing package that standardizes the responses like DRF Standardized Errors β€” DRF Standardized Errors documentation.

1 Like