Cache-Control Header Directives in REST API
📖 Scenario: You are building a simple REST API that returns user profile data. To improve performance and control how clients cache the responses, you need to add Cache-Control headers with specific directives.
🎯 Goal: Build a REST API endpoint that returns a JSON user profile and sets the Cache-Control header with the directive max-age=60 to tell clients to cache the response for 60 seconds.
📋 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" respectively.Create a variable called
cache_control_header and set it to the string "max-age=60".Create a function called
get_user_profile that returns a tuple with user_profile and a dictionary containing the Cache-Control header set to cache_control_header.Print the result of calling
get_user_profile().💡 Why This Matters
🌍 Real World
APIs use Cache-Control headers to tell browsers and clients how long to keep data before asking again. This helps speed up apps and reduce server load.
💼 Career
Understanding HTTP headers like Cache-Control is important for backend developers, API designers, and anyone working with web services.
Progress0 / 4 steps