How to return the status code in Flask

What is Flask?

Flask is a lightweight Python framework for quick and rapid web application development.

What are status codes in HTTP?

HTTP response status codes are used in web development to determine whether a particular HTTP request has been completed successfully.

Status code in a tuple

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:

  1. (response, status_code)
  2. (response, status_code, headers)

Code

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": "*"}

Explanation

In the code above, the important lines to focus on are the following:

  • Line 14: We are returning the response as a tuple with the response body as Success and the status code of 201.
  • Line 24: We are returning the response as a tuple with the response body as 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"

The make_response() method

The 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:

  1. Pass the status code as a constructor parameter.
  2. Use the property status_code to set the status code. Here, the value set has to be an integer.

Code


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

Explanation

In the code above, the important lines to focus on are the following:

  • Line 14: We use the make_response method to create an instance of the Response class. We pass the status code as a constructor parameter.
  • Lines 25–26: We use the 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"
Copyright ©2024 Educative, Inc. All rights reserved