0
0
Flaskframework~10 mins

Custom status codes in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom status codes
Client sends request
Flask route receives request
Process request logic
Create response with custom status code
Send response back to client
Client receives response with custom status code
The client sends a request, Flask processes it, then returns a response with a custom status code back to the client.
Execution Sample
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/custom')
def custom_status():
    return jsonify(message='Hello'), 202
This Flask route returns a JSON message with a custom status code 202 (Accepted).
Execution Table
StepActionStatus Code SetResponse ContentClient Receives
1Client sends GET /customN/AN/ARequest sent
2Flask route '/custom' calledN/AN/AWaiting for response
3Return jsonify with message and status 202202{"message": "Hello"}Response received
4Client processes response202{"message": "Hello"}Message shown with status 202
💡 Response sent with custom status code 202, ending request cycle
Variable Tracker
VariableStartAfter Step 3Final
status_codeNone202202
response_contentNone{"message": "Hello"}{"message": "Hello"}
Key Moments - 2 Insights
Why do we return a tuple with (response, status_code) in Flask?
Flask allows returning a tuple where the second item is the status code. This sets the HTTP status code explicitly, as shown in execution_table step 3.
Can we use any number as a custom status code?
You can use standard HTTP codes or custom ones, but it's best to use valid HTTP status codes. The example uses 202 which means 'Accepted'. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what status code is set at step 3?
A404
B202
C200
D500
💡 Hint
Check the 'Status Code Set' column at step 3 in the execution_table.
At which step does the client receive the response?
AStep 3
BStep 4
CStep 1
DStep 2
💡 Hint
Look at the 'Client Receives' column to find when the response is received.
If we changed the status code to 404, what would change in the execution_table?
AThe client would send a different request
BThe 'Response Content' would change to an error message
CThe 'Status Code Set' at step 3 would be 404
DNothing would change
💡 Hint
Focus on the 'Status Code Set' column at step 3 in the execution_table.
Concept Snapshot
Flask lets you return a response with a custom status code by returning a tuple: (response, status_code).
Use jsonify() to send JSON data.
Example: return jsonify(message='Hi'), 202
The status code tells the client how to interpret the response.
Common codes: 200 OK, 404 Not Found, 202 Accepted.
Custom codes should follow HTTP standards.
Full Transcript
In Flask, you can send a custom status code by returning a tuple from your route function. The first item is the response data, often created with jsonify for JSON responses. The second item is the status code number, like 202 for Accepted. When a client sends a request, Flask runs the route function, creates the response with the custom status code, and sends it back. The client then receives the response and can act based on the status code. This lets you communicate more precise information about the request result beyond the default 200 OK.