Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: How do I set the status code in Response

Everything runs fine, API(flask) returns the data nicely. However, when I try to customise the response code, I can't do it.
Following are the two ways, I tried:

from flask import make_response
dictionary = {1:'a', 2:'b'}
resp = make_response(dictionary,1001)
return resp

#In developer tools, I could see the data, but status-code is 200

.

from flask import Response
dictionary = {1:'a', 2:'b'}
resp = Response(dictionary, status = 1001)
return resp

#Here again, I see the data, but the status code is 200

How do I set the status code to be something else?

like image 711
Mr.President Avatar asked Mar 25 '26 10:03

Mr.President


1 Answers

This should answer your question : https://flask.palletsprojects.com/en/1.1.x/quickstart/#about-responses

Example from docs:

@app.errorhandler(404)
def not_found(error):
    resp = make_response(render_template('error.html'), 404)
    resp.headers['X-Something'] = 'A value'
    return resp
like image 177
marxmacher Avatar answered Mar 26 '26 23:03

marxmacher