0
0
Rest APIprogramming~30 mins

ETag for conditional requests in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
ETag for conditional requests
📖 Scenario: You are building a simple REST API that serves user profile data. To save bandwidth and improve performance, you want to use ETags to let clients check if the data has changed before downloading it again.
🎯 Goal: Create a REST API endpoint that returns user profile data with an ETag header. The API should check the client's If-None-Match header and respond with 304 Not Modified if the data has not changed.
📋 What You'll Learn
Create a dictionary called user_profile with keys id, name, and email and exact values 1, 'Alice', and 'alice@example.com'
Create a variable called etag that stores the ETag string value "abc123"
Write a function called get_user_profile that takes a parameter if_none_match and returns 304 if if_none_match equals etag, otherwise returns the user_profile dictionary and the etag
Print the result of calling get_user_profile with if_none_match set to "abc123"
💡 Why This Matters
🌍 Real World
ETags help web servers tell clients if data has changed, saving bandwidth and speeding up web apps.
💼 Career
Understanding ETags is important for backend developers working on APIs and web performance optimization.
Progress0 / 4 steps
1
Create the user profile data
Create a dictionary called user_profile with these exact entries: 'id': 1, 'name': 'Alice', and 'email': 'alice@example.com'
Rest API
Need a hint?

Use curly braces {} to create a dictionary with the exact keys and values.

2
Create the ETag variable
Create a variable called etag and set it to the string "abc123"
Rest API
Need a hint?

Use double quotes to create the string "abc123".

3
Write the function to handle conditional requests
Write a function called get_user_profile that takes a parameter if_none_match. Inside the function, if if_none_match equals etag, return 304. Otherwise, return a tuple with user_profile and etag.
Rest API
Need a hint?

Use an if statement to compare if_none_match with etag. Return 304 if they match, else return the data and etag.

4
Test the function with a matching ETag
Print the result of calling get_user_profile with if_none_match set to "abc123"
Rest API
Need a hint?

Call get_user_profile with the string "abc123" and print the result.