Query Parameter Versioning in a REST API
📖 Scenario: You are building a simple REST API that returns a greeting message. You want to support different versions of the API using query parameters.For example, /greet?version=1 returns a basic greeting, and /greet?version=2 returns a more detailed greeting.
🎯 Goal: Create a REST API endpoint /greet that reads the version query parameter and returns the correct greeting message based on the version number.
📋 What You'll Learn
Create a dictionary called
greetings with keys as version numbers (integers) and values as greeting messages (strings).Create a variable called
default_version set to 1.Write a function called
get_greeting that takes a version parameter and returns the greeting message from the greetings dictionary or a default message if the version is not found.Print the greeting message for the version passed as a query parameter.
💡 Why This Matters
🌍 Real World
APIs often need to support multiple versions so that older clients can still work while new features are added. Using query parameters for versioning is one simple way to do this.
💼 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