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