0
0
Rest APIprogramming~30 mins

Query parameter versioning in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the greetings dictionary
Create a dictionary called greetings with these exact entries: 1: 'Hello!', 2: 'Hello, welcome to version 2 of our API!'
Rest API
Need a hint?

Use curly braces {} to create a dictionary with keys 1 and 2 and their greeting messages.

2
Set the default version
Create a variable called default_version and set it to 1.
Rest API
Need a hint?

Just assign the number 1 to the variable default_version.

3
Write the get_greeting function
Write a function called get_greeting that takes a parameter version and returns the greeting message from the greetings dictionary if the version exists, or returns 'Hello!' if the version is not found.
Rest API
Need a hint?

Use the dictionary get method to return the greeting or a default message.

4
Print the greeting for a query parameter version
Assume a variable query_version holds the version number from the query parameter. Set query_version to 2. Then print the greeting message by calling get_greeting(query_version).
Rest API
Need a hint?

Assign 2 to query_version and print the result of get_greeting(query_version).