0
0
Flaskframework~20 mins

HTTP status codes for APIs in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Status Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
A500 when item does not exist, 200 when item exists
BAlways 200 regardless of item existence
C200 when item exists, 404 when item does not exist
D404 always, even if item exists
Attempts:
2 left
💡 Hint
Look at the return statements and the status codes they include.
📝 Syntax
intermediate
2: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
Areturn jsonify(new_item), 201
Breturn jsonify(new_item, 201)
Creturn jsonify(new_item).status_code(201)
Dreturn new_item, 201
Attempts:
2 left
💡 Hint
Check the syntax for returning a tuple with response and status code.
🔧 Debug
advanced
2: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'})
ABecause ZeroDivisionError is not caught properly
BBecause no status code is specified, Flask defaults to 200 OK
CBecause jsonify automatically sets status to 500 on errors
DBecause the route method is missing @app.errorhandler decorator
Attempts:
2 left
💡 Hint
Think about what happens if you don't specify a status code in Flask.
state_output
advanced
2: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
A200 OK
B500 Internal Server Error
C400 Bad Request
D201 Created
Attempts:
2 left
💡 Hint
Check the return statement after adding the item.
🧠 Conceptual
expert
2: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?
A204 No Content
B200 OK
C201 Created
D202 Accepted
Attempts:
2 left
💡 Hint
Think about status codes that indicate success but no response body.