0
0
Rest APIprogramming~10 mins

ETag for conditional requests 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 ETag header in the HTTP response.

Rest API
response.headers['ETag'] = [1]
Drag options to blanks, or click blank then click option'
A"12345abcde"
Betag_value
Crequest.headers['ETag']
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using the request header value instead of setting a new ETag.
Assigning None instead of a string.
2fill in blank
medium

Complete the code to check if the client's ETag matches the server's ETag.

Rest API
if request.headers.get('If-None-Match') == [1]:
Drag options to blanks, or click blank then click option'
Aresponse.headers['ETag']
BNone
C"12345abcde"
Drequest.headers['ETag']
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the request's own ETag header.
Using None instead of the ETag string.
3fill in blank
hard

Fix the error in the code to return 304 Not Modified when ETags match.

Rest API
if request.headers.get('If-None-Match') == [1]:
    return '', 304
Drag options to blanks, or click blank then click option'
A"12345abcde"
Bresponse.headers['ETag']
Crequest.headers['ETag']
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using request or response header objects instead of string.
Using None which causes errors.
4fill in blank
hard

Fill both blanks to generate an ETag from the content and check the client's ETag.

Rest API
import hashlib

content = b"Hello World"
etag = [1](content).hexdigest()
if request.headers.get('If-None-Match') == [2]:
    return '', 304
Drag options to blanks, or click blank then click option'
Ahashlib.md5
Betag
C"etag"
Dhashlib.sha256
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable for comparison.
Using the wrong hash function or variable names.
5fill in blank
hard

Fill all three blanks to set the ETag header, check the client's ETag, and return 304 if they match.

Rest API
import hashlib

content = b"Data to cache"
etag = [1](content).hexdigest()
response.headers['ETag'] = [2]
if request.headers.get('If-None-Match') == [3]:
    return '', 304
Drag options to blanks, or click blank then click option'
Ahashlib.sha1
Betag
C"etag"
Dhashlib.md5
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of variables for headers.
Mixing different hash functions.