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 etagPrint 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