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?
HTTP/1.1 304 Not Modified ETag: "abc123"
Think about what the server does when the client's ETag matches the current resource's ETag.
When the client's If-None-Match header matches the server's current ETag, the server responds with 304 Not Modified and no body, telling the client to use its cached copy.
In validation-based caching, clients send a header to check if their cached resource is still valid. Which header is it?
It is a header that asks the server if the resource has changed since a certain date.
The If-Modified-Since header is sent by clients to ask the server if the resource has changed since the given date. If not, the server can respond with 304 Not Modified.
Given this simplified server code snippet in Python Flask, why does the server never return 304 Not Modified?
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
Check how the ETag value is formatted and compared with the header.
The ETag header value is usually quoted (e.g., "xyz789"), but the code compares the raw string without quotes to the header value which includes quotes. This causes the comparison to fail and the server always sends full data.
Choose the correct way to set the ETag header in an Express.js response.
app.get('/resource', (req, res) => { const etagValue = 'abc123'; // Set ETag header here res.send('Hello World'); });
Express response objects have a method to set headers with case-insensitive names.
The res.set() method is the correct way to set headers in Express. It is case-insensitive and sets the header properly. res.header() is an alias but less recommended. res.setHeader() is not a method on Express response. res.etag is not a valid property.
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?
If the client's ETag matches the server's, the server responds with 304 Not Modified.
If all 3 ETags match, the server responds with 304 Not Modified for all 3 resources, so the client avoids downloading all 3 again.