Challenge - 5 Problems
Query Parameter Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check how the version parameter is read and matched in the code.
✗ Incorrect
The code reads the 'version' query parameter. If it is '2', it returns the message for version 2. Since the URL has version=2, option B is correct.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about client errors vs server errors.
✗ Incorrect
A 400 Bad Request indicates the client sent a request with invalid parameters, such as an unsupported version. This is more appropriate than 404 or 500.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check the data type of the version variable and the comparisons.
✗ Incorrect
request.args.get returns a string. Comparing string '2' to integer 2 is false, so the code always falls to else.
📝 Syntax
advanced1: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'
Attempts:
2 left
💡 Hint
Remember the difference between assignment and comparison operators.
✗ Incorrect
Option C uses a single equals sign (=) in the if condition, which is assignment and causes a syntax error. Conditions require == for comparison.
🚀 Application
expert2: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
Attempts:
2 left
💡 Hint
Count the distinct JSON responses for versions 1, 2, and 3 only.
✗ Incorrect
For versions 1, 2, and 3, the API returns three distinct JSON objects with 'data' keys. The error response is for other versions, not counted here.