Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change status of JsonResponse in Django

My API is returning a JSON object on error but the status code is HTTP 200:

response = JsonResponse({'status': 'false', 'message': message}) return response 

How can I change the response code to indicate an error?

like image 548
Dhanushka Amarakoon Avatar asked Jan 28 '16 11:01

Dhanushka Amarakoon


1 Answers

JsonResponse normally returns HTTP 200, which is the status code for 'OK'. In order to indicate an error, you can add an HTTP status code to JsonResponse as it is a subclass of HttpResponse:

response = JsonResponse({'status':'false','message':message}, status=500) 
like image 111
Selcuk Avatar answered Oct 08 '22 23:10

Selcuk