0
0
Rest APIprogramming~10 mins

Validation-based caching in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the cache control header for 60 seconds.

Rest API
response.headers['Cache-Control'] = '[1]'
Drag options to blanks, or click blank then click option'
Amax-age=60
Bno-cache
Cno-store
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'no-cache' disables caching instead of setting duration.
Using 'private' restricts caching to private caches only.
2fill in blank
medium

Complete the code to check if the ETag header matches the client's If-None-Match header.

Rest API
if request.headers.get('If-None-Match') == [1]:
Drag options to blanks, or click blank then click option'
Arequest.headers['ETag']
Bresponse.headers['ETag']
Cresponse.headers['Cache-Control']
Drequest.headers['Cache-Control']
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with request headers instead of response headers.
Using Cache-Control header instead of ETag.
3fill in blank
hard

Fix the error in setting the 304 Not Modified response status.

Rest API
if request.headers.get('If-None-Match') == response.headers.get('ETag'):
    return [1]
Drag options to blanks, or click blank then click option'
A500
B404
C200
D304
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 returns full content unnecessarily.
Using 404 or 500 are error codes, not cache-related.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that caches only responses with status 200.

Rest API
cache = {url: response for url, response in responses.items() if response.status_code [1] [2]
Drag options to blanks, or click blank then click option'
A==
B!=
C200
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' caches error responses.
Checking for 404 caches not found errors.
5fill in blank
hard

Fill all three blanks to build a dictionary comprehension caching responses with ETag and max-age > 0.

Rest API
valid_cache = {url: resp for url, resp in responses.items() if resp.headers.get([1]) and int(resp.headers.get([2], 0)) [3] 0}
Drag options to blanks, or click blank then click option'
A'ETag'
B'Cache-Control'
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for Cache-Control in place of ETag.
Using '<' instead of '>' for max-age comparison.