0
0
Rest APIprogramming~15 mins

Cache-Control header directives in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
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
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
Set the Cache-Control header directive
Create a variable called cache_control_header and set it to the string "max-age=60".
Rest API
Need a hint?

Assign the exact string "max-age=60" to the variable cache_control_header.

3
Create the API function with Cache-Control header
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.
Rest API
Need a hint?

Define a function that returns a tuple: the user_profile dictionary and a headers dictionary with the Cache-Control key.

4
Print the API response
Print the result of calling get_user_profile().
Rest API
Need a hint?

Use print(get_user_profile()) to show the returned data and headers.