admin管理员组文章数量:1317894
I tried using send_file
to send the file and setting custom headers, but Flask only allows returning one response body, so it seems like the headers are not being sent when the file is returned.
I tried using jsonify
to send the JSON response with headers, but I can't seem to combine that with the file in the same response.
from flask import Flask, send_file, jsonify
app = Flask(__name__)
@app.route('/download-pdf', methods=['GET'])
def download_pdf():
file_path = 'Frame_relate.pdf'
response = send_file(file_path, as_attachment=True, download_name="Frame_relate.pdf")
response.headers['Status'] = '200 OK'
additional_info = jsonify({"message": "File is being downloaded", "status": "success"})
return response, additional_info
if __name__ == '__main__':
app.run(debug=True)
I tried using send_file
to send the file and setting custom headers, but Flask only allows returning one response body, so it seems like the headers are not being sent when the file is returned.
I tried using jsonify
to send the JSON response with headers, but I can't seem to combine that with the file in the same response.
from flask import Flask, send_file, jsonify
app = Flask(__name__)
@app.route('/download-pdf', methods=['GET'])
def download_pdf():
file_path = 'Frame_relate.pdf'
response = send_file(file_path, as_attachment=True, download_name="Frame_relate.pdf")
response.headers['Status'] = '200 OK'
additional_info = jsonify({"message": "File is being downloaded", "status": "success"})
return response, additional_info
if __name__ == '__main__':
app.run(debug=True)
Share
Improve this question
edited Jan 22 at 15:35
VLAZ
29.1k9 gold badges63 silver badges84 bronze badges
asked Jan 22 at 15:21
RohithCodezRohithCodez
111 silver badge4 bronze badges
2
|
1 Answer
Reset to default 1jsonify
does return Response
object, headers values are limited to string (this is due to how HTTP works), so you should use json.dumps
if you wish to use JSON as serialization. You might set multiple headers for given Flask.Response
, consider following simple example
import json
from flask import Flask, make_response
app = Flask(__name__)
@app.route('/headers')
def headers():
file_path = 'Frame_relate.pdf'
response = make_response("I have headers")
response.headers['CustomOne'] = 'just text'
response.headers['CustomTwo'] = json.dumps({"message":"IamJSON"})
return response
if __name__ == '__main__':
app.run(debug=True)
then after starting server, result of wget -S --spider http://localhost:5000/headers
will contain
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: Werkzeug/2.3.6 Python/3.10.12
Date: Wed, 22 Jan 2025 16:03:45 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 14
CustomOne: just text
CustomTwo: {"message": "IamJSON"}
Connection: close
Note: I use make_response
for simplicity.
本文标签: pythonHow to add custom headers with JSON and file response in the same endpointStack Overflow
版权声明:本文标题:python - How to add custom headers with JSON and file response in the same endpoint? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742036625a2417333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
additional_info
doing for you here? It looks to me like you set the headerStatus
. – JonSG Commented Jan 22 at 16:00