0
0
Flaskframework~30 mins

Custom status codes in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom status codes in Flask
📖 Scenario: You are building a simple web API using Flask. You want to send custom HTTP status codes with your responses to indicate different situations clearly.
🎯 Goal: Create a Flask app that returns a JSON message with a custom status code when a specific route is accessed.
📋 What You'll Learn
Create a Flask app instance named app
Define a route /status that returns a JSON response
Use a custom status code 299 with the response
Return a JSON dictionary with a key message and value "Custom status code response"
💡 Why This Matters
🌍 Real World
Custom status codes help APIs communicate specific states or errors beyond standard HTTP codes, improving client-server communication.
💼 Career
Understanding how to send custom status codes is useful for backend developers building APIs that need precise control over response statuses.
Progress0 / 4 steps
1
Set up the Flask app
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use app = Flask(__name__) to create the app instance.

2
Create a route for /status
Define a route /status using @app.route('/status') and create a function called status that will handle requests to this route.
Flask
Need a hint?

Use the @app.route decorator to define the route and a function named status.

3
Return JSON with custom status code
Inside the status function, import jsonify from flask and return a JSON response with {"message": "Custom status code response"} and a custom status code 299.
Flask
Need a hint?

Use return jsonify({...}), 299 to send JSON with a custom status code.

4
Add the app run block
Add the standard Flask app run block: if __name__ == '__main__': and inside it call app.run() to start the server.
Flask
Need a hint?

This block runs the Flask app when the script is executed directly.