0
0
Rest APIprogramming~20 mins

Query parameter versioning in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Parameter Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this API version check?
Given this Python Flask snippet that checks API version from query parameters, what will be the response if the request URL is /api/resource?version=2?
Rest API
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/resource')
def resource():
    version = request.args.get('version', '1')
    if version == '1':
        return jsonify({'message': 'Version 1 response'})
    elif version == '2':
        return jsonify({'message': 'Version 2 response'})
    else:
        return jsonify({'error': 'Unsupported version'}), 400
A500 Internal Server Error
B{"message": "Version 2 response"}
C{"error": "Unsupported version"}
D{"message": "Version 1 response"}
Attempts:
2 left
💡 Hint
Check how the version parameter is read and matched in the code.
🧠 Conceptual
intermediate
1:30remaining
Which HTTP status code is best for unsupported API version?
When a client requests an API version that the server does not support using query parameter versioning, which HTTP status code should the server return?
A400 Bad Request
B404 Not Found
C200 OK
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about client errors vs server errors.
🔧 Debug
advanced
2:30remaining
Why does this version check always return unsupported version response?
This Flask code is intended to return different responses based on the 'version' query parameter. But it always returns the unsupported version response, even when version=2 is requested. What is the bug?
Rest API
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/data')
def data():
    version = request.args.get('version')
    if version == 1:
        return jsonify({'message': 'Version 1 data'})
    elif version == 2:
        return jsonify({'message': 'Version 2 data'})
    else:
        return jsonify({'error': 'Unsupported version'}), 400
AThe version variable is a string, but compared to integers 1 and 2, so conditions never match.
BThe else block is missing a return statement.
CThe jsonify function is used incorrectly without parentheses.
DThe route decorator is missing a trailing slash causing routing issues.
Attempts:
2 left
💡 Hint
Check the data type of the version variable and the comparisons.
📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in this version check?
Identify which code snippet will cause a syntax error when checking the API version from query parameters.
Rest API
version = request.args.get('version', '1')
if version == '1':
    return 'v1'
elif version == '2':
    return 'v2'
else:
    return 'unsupported'
A
if version == '1':
    return 'v1'
Bif version == '1': return 'v1'
Cif version = '1': return 'v1'
D
if version == '1'
    return 'v1'
Attempts:
2 left
💡 Hint
Remember the difference between assignment and comparison operators.
🚀 Application
expert
2:30remaining
How many distinct responses does this API produce for versions 1 to 3?
Consider this Flask API code snippet that uses query parameter versioning. How many distinct JSON responses can this API produce when the version parameter is 1, 2, or 3?
Rest API
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/item')
def item():
    version = request.args.get('version', '1')
    match version:
        case '1':
            return jsonify({'data': 'Item v1'})
        case '2':
            return jsonify({'data': 'Item v2'})
        case '3':
            return jsonify({'data': 'Item v3'})
        case _:
            return jsonify({'error': 'Unsupported version'}), 400
A4
B0
C1
D3
Attempts:
2 left
💡 Hint
Count the distinct JSON responses for versions 1, 2, and 3 only.