我将python金字塔项目从python 2移动到3.我使用ReportLab生成PDF文件并将其发送到前端。根据他们的示例,我需要使用io.BytesIO()
,之前它是StringIO()
。
现在使用生成的文档长度在我的回复中设置Content-Length
,我收到HPE_UNEXPECTED_CONTENT_LENGTH
错误。
pdf = io.BytesIO()
doc = SimpleDocTemplate(pdf)
doc.build(story)
pdfcontent = pdf.getvalue()
pdf.close()
response = Response(content_type='application/pdf', body=pdfcontent)
response.headers.add("Content-Length", str(len(pdfcontent)))
如果我没有设置Content-Length
属性,下载工作正常,但我不想留空。
答案 0 :(得分:1)
我不确定您的具体示例和错误,但我非常确定当您提供这样的响应body
字节时,Pyramid会发送Content-Length
标头。无需手动设置,它已经有字节,因此知道它的大小。
您应该检查响应标头(使用浏览器开发人员工具或curl或httpie等命令行工具)。