Как правильно получить вложение pdf из GET-запроса в django
У меня есть такой простой вид:
class Reports(generics.GenericAPIView):
permission_classes = (permissions.AllowAny,)
def get(self, request, *args, **kwargs):
report_id = request.GET.get("report_id")
response = get_report(report_id)
return Response(response, status=status.HTTP_200_OK)
Метод get_report просто удаляет полезную нагрузку и возвращает только id, который нужно отправить для получения pdf.
Ошибка, которая возвращается:
ERROR 2022-04-20 19:02:58,537 log 148558 140052495332928 Internal Server Error: /api/v2/app/reports
Traceback (most recent call last):
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/rest_framework/response.py", line 70, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/rest_framework/renderers.py", line 103, in render
allow_nan=not self.strict, separators=separators
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/rest_framework/utils/json.py", line 25, in dumps
return json.dumps(*args, **kwargs)
File "/usr/local/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/rest_framework/utils/encoders.py", line 50, in default
return obj.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 10: invalid start byte
Заголовки ответа сервера:
access-control-allow-origin: *
access-control-expose-headers: content-disposition
content-disposition: attachment; filename="assessment_hbx0MkrjYad2I2suaKcrIdzOcaMlIBSGDw4I8TBprWC28GbDFkF0KxuBpNMB1qYc.pdf"
content-length: 48818
content-type: application/octet-stream
date: Wed,20 Apr 2022 19:16:34 GMT
server: istio-envoy
x-envoy-upstream-service-time: 2099
x-powered-by: Express
Filename уникально для каждой загрузки, поэтому оно постоянно меняется в ответе сервера.
Спасибо за предложение.
Используйте httprespons: например
return HttpResponse(pdf_data, content_type="application/pdf")