0

I'm trying to return custom headers like this:

@app.route("/generate", methods=['POST'])
def generate():
    # data is generated here
    resp = Response(jsonify(data))
    resp.headers['Access-Control-Allow-Origin'] = '*'
    resp.headers['Access-Control-Allow-Credentials'] = True
    return resp

But they don't seem to be present in the output. Changing status code to 201 as mentioned here didn't help either (I did this by adding status=201 to Responce construction).

Any bit of help would be appreciated.

EDIT 1: Added a bit to a code. Solutions described here didn't work for me either. Tried both Response and make_response, added @app.after_request as suggested - no effect. The only headers present are:

'Content-Type': 'application/json', 
'Content-Length': '113', 
'Server': 'Werkzeug/1.0.1 Python/3.7.7', 
'Date': 'Fri, 08 May 2020 17:05:38 GMT'

1 Answers1

0

I think the problem is that, although you are adding custom headers, you are still returning a JSON object where you write resp = Response(jsonify(data)). It is possible that flask assingns the response headers automatically based on the response's body's format.

Foivoschr
  • 455
  • 1
  • 4
  • 14