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
  • What is additional_info doing for you here? It looks to me like you set the header Status. – JonSG Commented Jan 22 at 16:00
  • 1 This question is similar to: How to send header in flask send_file?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – JonSG Commented Jan 22 at 16:01
Add a comment  | 

1 Answer 1

Reset to default 1

jsonify 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