Challenge - 5 Problems
Header 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 Flask API versioning code?
Consider this Flask API snippet using header-based versioning. What JSON response will the client receive when sending a request with header 'Accept-Version: v2'?
Rest API
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/data') def data(): version = request.headers.get('Accept-Version', 'v1') if version == 'v1': return jsonify({'message': 'Data from version 1'}) elif version == 'v2': return jsonify({'message': 'Data from version 2', 'extra': 'Additional info'}) else: return jsonify({'error': 'Unsupported version'}), 400 # Assume client sends header: Accept-Version: v2
Attempts:
2 left
💡 Hint
Check the header value and which branch the code executes.
✗ Incorrect
The code checks the 'Accept-Version' header. If it is 'v2', it returns the JSON with message 'Data from version 2' and extra info. Other versions return different responses.
🧠 Conceptual
intermediate1:00remaining
Which header is commonly used for header-based API versioning?
In header-based versioning for REST APIs, which HTTP header is typically used to specify the API version?
Attempts:
2 left
💡 Hint
It is a custom header indicating the version the client wants.
✗ Incorrect
The 'Accept-Version' header is commonly used to specify the API version in header-based versioning.
🔧 Debug
advanced1:30remaining
What error does this header-based versioning code raise?
This code snippet tries to read the API version from headers but has a bug. What error occurs when a request without the 'Accept-Version' header is sent?
Rest API
def get_version(request): version = request.headers['Accept-Version'] if version == 'v1': return 'Version 1' else: return 'Other version' # Request without 'Accept-Version' header
Attempts:
2 left
💡 Hint
Accessing a missing dictionary key without a default causes an error.
✗ Incorrect
Accessing request.headers['Accept-Version'] when the header is missing raises a KeyError.
📝 Syntax
advanced1:30remaining
Which option correctly extracts the API version from headers with a default fallback?
Choose the code snippet that safely gets the 'Accept-Version' header from a request, defaulting to 'v1' if missing.
Attempts:
2 left
💡 Hint
Use the dictionary get method with a default value.
✗ Incorrect
Option B uses the .get() method with a default, which safely returns 'v1' if the header is missing.
🚀 Application
expert2:00remaining
How many different JSON keys are in the combined responses of this header-based versioning API?
Given this API code, how many unique JSON keys appear in all possible successful responses combined?
Rest API
def api_response(version): if version == 'v1': return {'status': 'ok', 'data': [1, 2, 3]} elif version == 'v2': return {'status': 'ok', 'data': [1, 2, 3], 'meta': {'count': 3}} elif version == 'v3': return {'status': 'ok', 'items': [1, 2, 3], 'meta': {'count': 3}} else: return {'error': 'Invalid version'} # Consider only successful responses (no 'error' key)
Attempts:
2 left
💡 Hint
List all keys from each successful response and count unique ones.
✗ Incorrect
Successful responses have keys: v1: status, data; v2: status, data, meta; v3: status, items, meta. Unique keys are status, data, meta, items → 4 keys total. But careful: v1 has 2 keys, v2 adds meta (3 keys), v3 replaces data with items (still unique), so total unique keys are status, data, meta, items → 4 keys. The question asks for combined unique keys, so answer is 4.