Custom status codes let you send special messages from your web server to the browser or client. This helps explain what happened with a request beyond the usual success or error.
Custom status codes in Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/example') def example(): return jsonify(message='Custom status code example'), 299
You return a tuple with the response data and the custom status code number.
Status codes should be integers between 100 and 599.
return 'Processing', 102
return jsonify(error='Custom error'), 499
return '', 204
This Flask app has one route '/custom' that sends a JSON message with a custom status code 299. When you visit this URL, the server responds with the message and the special code.
from flask import Flask, jsonify app = Flask(__name__) @app.route('/custom') def custom_status(): # Return a JSON message with a custom status code 299 return jsonify(message='This is a custom status code'), 299 if __name__ == '__main__': app.run(debug=True)
Browsers may not recognize custom status codes, so use them mainly for API clients or internal tools.
Always use standard codes when possible for better compatibility.
Custom codes should be documented clearly for anyone using your API.
Custom status codes let your server send special signals beyond standard HTTP codes.
In Flask, return a tuple with data and the custom code number.
Use custom codes carefully and document them well.
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
