0
0
Rest APIprogramming~20 mins

Validation-based caching in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Validation-based caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this HTTP cache validation example?

Consider a REST API that uses ETag headers for validation-based caching. The server responds with an ETag value "abc123" for a resource. A client sends a GET request with header If-None-Match: "abc123". What will the server respond?

Rest API
HTTP/1.1 304 Not Modified
ETag: "abc123"

AHTTP 404 Not Found
BHTTP 500 Internal Server Error
CHTTP 304 Not Modified with no body
DHTTP 200 OK with full resource body
Attempts:
2 left
💡 Hint

Think about what the server does when the client's ETag matches the current resource's ETag.

🧠 Conceptual
intermediate
1:30remaining
Which HTTP header is used by clients to validate cached resources?

In validation-based caching, clients send a header to check if their cached resource is still valid. Which header is it?

AIf-Modified-Since
BCache-Control
CAuthorization
DContent-Type
Attempts:
2 left
💡 Hint

It is a header that asks the server if the resource has changed since a certain date.

🔧 Debug
advanced
2:30remaining
Why does this validation-based caching code always return full data instead of 304?

Given this simplified server code snippet in Python Flask, why does the server never return 304 Not Modified?

Rest API
from flask import Flask, request, jsonify, make_response
app = Flask(__name__)

@app.route('/data')
def data():
    etag = '"xyz789"'
    if request.headers.get('If-None-Match') == etag:
        return make_response('', 304)
    response = make_response(jsonify({'value': 42}))
    response.headers['ETag'] = etag
    return response
AThe response body is always sent regardless of ETag
BThe server does not check the If-Modified-Since header
CThe ETag header is not set in the response
DThe If-None-Match header is compared incorrectly (missing quotes)
Attempts:
2 left
💡 Hint

Check how the ETag value is formatted and compared with the header.

📝 Syntax
advanced
2:00remaining
Which option correctly sets the ETag header in this Node.js Express code?

Choose the correct way to set the ETag header in an Express.js response.

Rest API
app.get('/resource', (req, res) => {
  const etagValue = 'abc123';
  // Set ETag header here
  res.send('Hello World');
});
Ares.set('ETag', etagValue);
Bres.setHeader('etag', etagValue);
Cres.header('ETag', etagValue);
Dres.etag = etagValue;
Attempts:
2 left
💡 Hint

Express response objects have a method to set headers with case-insensitive names.

🚀 Application
expert
1:30remaining
How many items are cached when using validation-based caching with ETag and If-None-Match?

A REST API serves 3 different resources, each with unique ETags. A client caches all 3 resources and sends requests with If-None-Match headers for each. How many resources will the client avoid downloading if all ETags match?

A0
B3
C2
D1
Attempts:
2 left
💡 Hint

If the client's ETag matches the server's, the server responds with 304 Not Modified.