0
0
Rest APIprogramming~30 mins

Header-based versioning in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Header-based Versioning in a REST API
📖 Scenario: You are building a simple REST API for a book store. You want to support different versions of the API using HTTP headers. This way, clients can specify which version of the API they want to use by sending a custom header.
🎯 Goal: Create a REST API endpoint that returns book information. Implement header-based versioning so that when the client sends X-API-Version: 1, the API returns the book title only, and when the client sends X-API-Version: 2, the API returns the book title and author.
📋 What You'll Learn
Create a dictionary called book with keys title and author and values 'The Great Gatsby' and 'F. Scott Fitzgerald' respectively.
Create a variable called version_header that simulates reading the 'X-API-Version' header from a request.
Use an if-elif-else statement to check the value of version_header and create a dictionary called response with the correct data for version 1 or 2.
Print the response dictionary.
💡 Why This Matters
🌍 Real World
APIs often need to support multiple versions so that old clients keep working while new features are added. Header-based versioning is one way to do this cleanly.
💼 Career
Understanding how to handle API versioning is important for backend developers and anyone working with web services to ensure smooth upgrades and backward compatibility.
Progress0 / 4 steps
1
DATA SETUP: Create the book data
Create a dictionary called book with these exact entries: 'title': 'The Great Gatsby' and 'author': 'F. Scott Fitzgerald'.
Rest API
Need a hint?

Use curly braces {} to create a dictionary with the keys and values exactly as shown.

2
CONFIGURATION: Simulate reading the version header
Create a variable called version_header and set it to the string '1' to simulate the 'X-API-Version' header value.
Rest API
Need a hint?

Assign the string '1' to the variable version_header.

3
CORE LOGIC: Create response based on version header
Use an if-elif-else statement to check if version_header is '1' or '2'. Create a dictionary called response that contains only 'title' from book if version is '1', or both 'title' and 'author' if version is '2'. If the version is neither, set response to {'error': 'Unsupported API version'}.
Rest API
Need a hint?

Use if to check for version '1', elif for version '2', and else for unsupported versions.

4
OUTPUT: Print the response
Write a print statement to display the response dictionary.
Rest API
Need a hint?

Use print(response) to show the final output.