📖 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
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
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
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
Hint
This block runs the Flask app when the script is executed directly.
Practice
(1/5)
1. What is the correct way to return a custom status code 450 from a Flask route?
easy
A. return "Error", 450
B. return 450, "Error"
C. return Response(status=450)
D. return make_response(450, "Error")
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 A
Quick Check:
Tuple (body, status) = return "Error", 450 [OK]
Hint: Return (body, code) tuple to set custom status [OK]
Common Mistakes:
Swapping order of status and body in return tuple
Using Response without import or wrong parameters
Using make_response incorrectly
2. Which of the following is the correct syntax to return a JSON response with a custom status code 499 in Flask?
easy
A. return jsonify(message="Fail"), 499
B. return jsonify(message="Fail", status=499)
C. return jsonify(message="Fail", 499)
D. return jsonify(message="Fail").status_code(499)
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.
D. Using 'status' keyword argument in return is invalid
Solution
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 D
Quick Check:
Return tuple (body, code) not keyword args [OK]
Hint: Return tuple (body, code), not keyword args [OK]
Common Mistakes:
Using status=code in return instead of tuple
Assuming Flask auto-detects status from keywords
Confusing with Response object parameters
5. You want to create a Flask route that returns a JSON message with a custom status code 460 only if a query parameter 'valid' is 'no'. Otherwise, return status 200 with a success message. Which code correctly implements this?
hard
A. from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/check')
def check():
if request.args.get('valid') == 'no':
return jsonify(error="Invalid", status=460)
return jsonify(message="OK")
B. from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/check')
def check():
if request.args.get('valid') == 'no':
return jsonify(error="Invalid"), 460
return jsonify(message="OK"), 200
C. from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/check')
def check():
if request.args.get('valid') == 'no':
return jsonify(error="Invalid").status_code = 460
return jsonify(message="OK")
D. from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/check')
def check():
if request.args.get('valid') == 'no':
return jsonify(error="Invalid")
return jsonify(message="OK"), 200
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 B
Quick Check:
Return (jsonify, code) tuples for all cases [OK]
Hint: Return (jsonify(...), code) tuples for all responses [OK]
Common Mistakes:
Passing status inside jsonify arguments
Trying to set status_code attribute directly
Missing status code tuple on error response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.