0
0
Rest APIprogramming~20 mins

Header-based versioning in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Header 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 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
A{"message": "Data from version 2", "extra": "Additional info"}
BHTTP 404 Not Found
C{"error": "Unsupported version"}
D{"message": "Data from version 1"}
Attempts:
2 left
💡 Hint
Check the header value and which branch the code executes.
🧠 Conceptual
intermediate
1: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?
AAuthorization
BContent-Type
CAccept-Version
DUser-Agent
Attempts:
2 left
💡 Hint
It is a custom header indicating the version the client wants.
🔧 Debug
advanced
1: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
AKeyError
BTypeError
CValueError
DNo error, returns 'Other version'
Attempts:
2 left
💡 Hint
Accessing a missing dictionary key without a default causes an error.
📝 Syntax
advanced
1: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.
Aversion = request.headers['Accept-Version'] or 'v1'
Bversion = request.headers.get('Accept-Version', 'v1')
Cversion = request.headers.get('Accept-Version') if 'Accept-Version' in request.headers else 'v1'
Dversion = request.headers['Accept-Version'] if request.headers['Accept-Version'] else 'v1'
Attempts:
2 left
💡 Hint
Use the dictionary get method with a default value.
🚀 Application
expert
2: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)
A6
B5
C3
D4
Attempts:
2 left
💡 Hint
List all keys from each successful response and count unique ones.