Media Type Versioning in REST API
📖 Scenario: You are building a simple REST API for a book store. You want to support versioning of your API using media type versioning. This means clients specify the API version in the Accept header using custom media types.For example, application/vnd.bookstore.v1+json for version 1 and application/vnd.bookstore.v2+json for version 2.
🎯 Goal: Create a basic REST API endpoint that returns book data. Implement media type versioning so the API returns different responses depending on the version specified in the Accept header.
📋 What You'll Learn
Create a dictionary called
books with two entries: "id": 1, "title": "The Great Gatsby" and "id": 2, "title": "1984".Create a variable called
supported_versions that lists the supported media types for version 1 and version 2.Write a function called
get_books that takes a media_type string and returns the book data formatted differently depending on the version in the media type.Print the output of
get_books when called with "application/vnd.bookstore.v1+json" and "application/vnd.bookstore.v2+json".💡 Why This Matters
🌍 Real World
Media type versioning helps APIs evolve without breaking existing clients by letting them request specific versions.
💼 Career
Understanding API versioning is important for backend developers and API designers to maintain and improve web services safely.
Progress0 / 4 steps