How to solve TypeError: Object of type HttpResponse is not JSON serializable?
I am trying to return a model.pkl file using postman from an API made in django rest framework. However the response I get is TypeError: Object of type HttpResponse is not JSON serializable
.
The following code is what I am using to return the file, but I cannot understand what is going wrong (this was inspired by this post).
from rest_framework.status import HTTP_200_OK
import pickle
from django.http import HttpResponse
from wsgiref.util import FileWrapper
# views.py
...
model_pickle_path = f'/path/model_state.pkl'
#model_pickle_trained = pickle.load(open(model_pickle_path, 'rb'))
model_pickle_trained = open(model_pickle_path, 'rb')
response = HttpResponse(FileWrapper(model_pickle_trained), content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=%s' % model_filename_defined
return Response(data=response, status=HTTP_200_OK)
Traceback:
TypeError: Object of type HttpResponse is not JSON serializable