1

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
  • I'm having the same problem. Do tell me if you find a solution Commented Aug 1, 2024 at 16:44

1 Answer 1

-1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.