Challenge - 5 Problems
HTTP Status Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What HTTP status code does this Flask API return?
Consider this Flask route. What HTTP status code will the API respond with when accessed?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/item/<int:id>') def get_item(id): items = {1: 'apple', 2: 'banana'} if id in items: return jsonify({'item': items[id]}), 200 else: return jsonify({'error': 'Item not found'}), 404
Attempts:
2 left
💡 Hint
Look at the return statements and the status codes they include.
✗ Incorrect
The route returns 200 OK with the item data if the item exists. If the item ID is not found, it returns 404 Not Found with an error message.
📝 Syntax
intermediate2:00remaining
Identify the correct way to return a 201 Created status in Flask
Which option correctly returns a JSON response with HTTP status 201 Created after creating a resource?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/create', methods=['POST']) def create(): new_item = {'id': 3, 'name': 'pear'} # Return response here
Attempts:
2 left
💡 Hint
Check the syntax for returning a tuple with response and status code.
✗ Incorrect
In Flask, to return a JSON response with a specific status code, you return a tuple: (response, status_code). Option A correctly does this.
🔧 Debug
advanced2:00remaining
Why does this Flask route always return 200 OK even on error?
Examine this Flask route. Why does it always return HTTP 200 OK even when an error occurs?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/divide/<int:a>/<int:b>') def divide(a, b): try: result = a / b return jsonify({'result': result}) except ZeroDivisionError: return jsonify({'error': 'Cannot divide by zero'})
Attempts:
2 left
💡 Hint
Think about what happens if you don't specify a status code in Flask.
✗ Incorrect
Flask returns 200 OK by default if no status code is given. The error JSON is returned but with status 200, which is misleading.
❓ state_output
advanced2:00remaining
What is the HTTP status code after this Flask POST request?
Given this Flask route, what HTTP status code will the client receive after a successful POST request?
Flask
from flask import Flask, request, jsonify app = Flask(__name__) items = [] @app.route('/items', methods=['POST']) def add_item(): data = request.get_json() if not data or 'name' not in data: return jsonify({'error': 'Missing name'}), 400 items.append(data['name']) return jsonify({'message': 'Item added'}), 201
Attempts:
2 left
💡 Hint
Check the return statement after adding the item.
✗ Incorrect
The route returns 201 Created after successfully adding the item. It returns 400 if the 'name' is missing.
🧠 Conceptual
expert2:00remaining
Which HTTP status code best fits this API scenario?
You have an API endpoint that updates a user's profile. The update is successful but does not return any content. Which HTTP status code should the API return?
Attempts:
2 left
💡 Hint
Think about status codes that indicate success but no response body.
✗ Incorrect
204 No Content means the request was successful but there is no content to return. It's ideal for update operations that don't return data.