Challenge - 5 Problems
Custom Status Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the HTTP status code returned by this Flask route?
Consider this Flask route that returns a custom status code.
What status code will the client receive when accessing this route?
What status code will the client receive when accessing this route?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/custom') def custom_status(): return jsonify(message='Hello'), 202 # Assume app is running and /custom is accessed
Attempts:
2 left
💡 Hint
Look at the second value returned by the route function.
✗ Incorrect
The route returns a tuple with the JSON response and the status code 202, so the client receives HTTP 202 Accepted.
📝 Syntax
intermediate1:30remaining
Which option correctly sets a custom status code in Flask?
You want to return a JSON response with a custom status code 418.
Which code snippet correctly does this?
Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Flask routes can return a tuple with response and status code.
✗ Incorrect
Option C returns a tuple (response, status_code) which Flask understands. Other options either misuse parameters or syntax.
🔧 Debug
advanced2:00remaining
Why does this Flask route always return status 200 instead of 404?
Examine the code below:
```python @app.route('/item/')
def get_item(id):
item = find_item(id)
if not item:
return {'error': 'Not found'}, 404
return item
```
Despite returning 404 in the code, the client always receives 200. Why?
```python @app.route('/item/
Despite returning 404 in the code, the client always receives 200. Why?
Attempts:
2 left
💡 Hint
Check what the return type is in each branch.
✗ Incorrect
Returning a dict directly sets status 200. The 404 tuple is only returned if item is None. If item is a dict, Flask returns 200 by default.
❓ state_output
advanced1:30remaining
What is the response status code after calling this Flask route twice?
Given this Flask app snippet:
```python from flask import Flask, jsonify app = Flask(__name__) counter = 0 @app.route('/count') def count(): global counter counter += 1 if counter > 1: return jsonify(message='Too many requests'), 429 return jsonify(message='First request'), 200 ```
What status code does the client receive on the second call to /count?
```python from flask import Flask, jsonify app = Flask(__name__) counter = 0 @app.route('/count') def count(): global counter counter += 1 if counter > 1: return jsonify(message='Too many requests'), 429 return jsonify(message='First request'), 200 ```
What status code does the client receive on the second call to /count?
Attempts:
2 left
💡 Hint
Look at how the counter variable changes with each call.
✗ Incorrect
On the first call, counter is 1, so status 200 is returned. On the second call, counter is 2, triggering status 429.
🧠 Conceptual
expert2:00remaining
Which statement about custom HTTP status codes in Flask is true?
Select the correct statement about using custom HTTP status codes in Flask responses.
Attempts:
2 left
💡 Hint
Think about HTTP protocol and client behavior.
✗ Incorrect
Flask allows any integer as status code, but clients may not handle unknown codes properly. Flask does not restrict codes or require registration.