Flask is a lightweight Python framework for quick and rapid web application development.
HTTP response status codes are used in web development to determine whether a particular HTTP request has been completed successfully.
The view
function can return the status code as one of a tuple’s elements. A response object is automatically created from the return result of a view
function. It is possible to get a tuple back that contains additional information. The tuple with the status code to be returned can be in any of the following formats:
(response, status_code)
(response, status_code, headers)
from flask import Flask, make_response, request app = Flask(__name__) @app.route("/userDetails", methods=["GET", "POST"]) def user_details(): if request.method == "POST": username = request.form.get("username") firstname = request.form.get("firstname") lastname = request.form["lastname"] return "Success", 201 @app.route("/userSignUp", methods=["POST"]) def sign_up(): if request.method == "POST": username = request.form.get("username") password = request.form.get("password") return "Success", 200, {"Access-Control-Allow-Origin": "*"}
In the code above, the important lines to focus on are the following:
Success
and the status code of 201
.Success
, status code of 200
, and some headers as a dictionary.Use the following curl
commands to test the userDetails
endpoint.
curl -X POST http://localhost:5000/userDetails -H "Content-Type: application/x-www-form-urlencoded" -d "username=sam&firstname=john&lastname=king"
Use the following curl
commands to test the userSignUp
endpoint.
curl -X POST http://localhost:5000/userSignUp -H "Content-Type: application/x-www-form-urlencoded" -d "username=sam&firstname=john"
make_response()
methodThe make_response
method from Flask returns a Response
object that can be used to send custom headers and change the properties such as status_code, mimetype, and so on.
We can set the status code using the make_response
method in two ways:
status_code
to set the status code. Here, the value set has to be an integer.from flask import Flask, make_response, request app = Flask(__name__) @app.route("/userDetails", methods=["GET", "POST"]) def user_details(): if request.method == "POST": username = request.form.get("username") firstname = request.form.get("firstname") lastname = request.form["lastname"] response = make_response("<h1>Success</h1>", 201) return response @app.route("/userSignUp", methods=["POST"]) def sign_up(): if request.method == "POST": username = request.form.get("username") password = request.form.get("password") response = make_response("<h1>Success</h1>") response.status_code = 200 return response
In the code above, the important lines to focus on are the following:
make_response
method to create an instance of the Response
class. We pass the status code as a constructor parameter.make_response
method to create an instance of the Response
class. We set the status code by explicitly setting the status_code
property of the response object.Use the following curl
commands to test the userDetails
endpoint.
curl -X POST http://localhost:5000/userDetails -H "Content-Type: application/x-www-form-urlencoded" -d "username=sam&firstname=john&lastname=king"
Use the following curl
commands to test the userSignUp
endpoint.
curl -X POST http://localhost:5000/userSignUp -H "Content-Type: application/x-www-form-urlencoded" -d "username=sam&firstname=john"
Free Resources