What if your app could speak a clearer language to users and clients with just a simple number?
Why Custom status codes in Flask? - Purpose & Use Cases
Imagine building a web app where you want to tell users exactly what happened with their request, but you only use the standard HTTP status codes like 200 or 404.
You try to explain special cases, like "payment pending" or "feature disabled," but the browser and clients only see generic codes.
Using only standard status codes makes your app responses unclear and confusing.
Clients can't easily understand special situations, so you end up writing extra code to parse messages or add confusing workarounds.
This slows down development and makes your API harder to maintain.
Custom status codes let you define clear, meaningful responses beyond the standard ones.
With Flask, you can send these codes easily, so clients immediately know the exact result of their request without guessing.
return jsonify({'error': 'Payment pending'}), 200
return jsonify({'error': 'Payment pending'}), 102
Custom status codes make your API communication precise and meaningful, improving client understanding and user experience.
Imagine an online store where orders can be "processing," "shipped," or "on hold." Custom status codes let the app tell the client exactly which state the order is in, so the user sees clear updates without confusion.
Standard codes are limited and can cause confusion.
Custom status codes provide clear, specific communication.
Flask makes it easy to send these codes in your responses.
Practice
Solution
Step 1: Understand Flask return tuple format
Flask routes return a tuple with (response_body, status_code) to set custom status codes.Step 2: Match the tuple with custom code 450
Returning "Error", 450 matches the format (body, status_code) correctly.Final Answer:
return "Error", 450 -> Option AQuick Check:
Tuple (body, status) = return "Error", 450 [OK]
- Swapping order of status and body in return tuple
- Using Response without import or wrong parameters
- Using make_response incorrectly
Solution
Step 1: Use jsonify to create JSON response
jsonify returns a Response object with JSON content.Step 2: Return tuple with jsonify and status code
To set custom status code, return (jsonify(...), status_code) tuple.Final Answer:
return jsonify(message="Fail"), 499 -> Option AQuick Check:
jsonify + tuple (response, code) = return jsonify(message="Fail"), 499 [OK]
- Passing status code inside jsonify arguments
- Trying to call status_code as method
- Incorrect tuple order or syntax
from flask import Flask
app = Flask(__name__)
@app.route('/test')
def test():
return "Custom", 499Solution
Step 1: Check the return statement in route
The route returns a tuple ("Custom", 499), where 499 is the status code.Step 2: Flask uses the second tuple element as status code
Flask sets HTTP status code to 499 as specified in the return tuple.Final Answer:
499 -> Option CQuick Check:
Return tuple second value = status code 499 [OK]
- Assuming default 200 status code
- Confusing return order
- Thinking 499 is invalid and defaults to 500
from flask import Flask
app = Flask(__name__)
@app.route('/error')
def error():
return "Oops", status=499Solution
Step 1: Check return statement syntax
Flask does not accept 'status' keyword in return statement directly.Step 2: Correct way is to return tuple (body, status_code)
Use return "Oops", 499 instead of return "Oops", status=499.Final Answer:
Using 'status' keyword argument in return is invalid -> Option DQuick Check:
Return tuple (body, code) not keyword args [OK]
- Using status=code in return instead of tuple
- Assuming Flask auto-detects status from keywords
- Confusing with Response object parameters
Solution
Step 1: Check conditional logic for query parameter
Use request.args.get('valid') to check if it equals 'no'.Step 2: Return JSON with custom status code 460 if invalid
Return (jsonify(...), 460) tuple to set custom status code correctly.Step 3: Return success JSON with status 200 otherwise
Return (jsonify(...), 200) tuple for success response.Final Answer:
Correct conditional returns with tuples for custom and normal codes -> Option BQuick Check:
Return (jsonify, code) tuples for all cases [OK]
- Passing status inside jsonify arguments
- Trying to set status_code attribute directly
- Missing status code tuple on error response
Ready to prove it?
Pass the test and this pattern moves unknown
