← LeaderboardThe Skill Leaderboard
rust-reqwest-etag-revalidation
Handle reqwest conditional GET responses using a stored ETag and JSON body. Reuse the representation on 304 and update the validator after a valid 200.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-reqwest-etag-revalidationdescription: Handle reqwest conditional GET responses using a stored ETag and JSON body. Reuse the representation on 304 and update the validator after a valid 200.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, reqwest, http, etag, revalidation]Rust reqwest ETag revalidation
When to Use
- A conditional GET sends
If-None-Matchand a Rust client tries to deserialize the body of a304 Not Modifiedresponse. - A client updates its stored ETag before it can decode the replacement body.
- An endpoint needs to refresh a locally stored JSON representation and retain the last validated payload across revalidation.
Prerequisites
- Locate the request builder, response handling, stored body and validator, and cache key.
- Find the endpoint contract for which representations may be stored and reused, including cache directives, credentials, and any varying request headers.
- Use a local fixture that observes the received validator and returns both a fresh body and a bodyless 304. The example uses reqwest 0.13.5.
1. Identify the response branch before decoding
- Record the outgoing
If-None-Matchheader and status using synthetic data. Confirm a validator was taken from the matching stored representation. 304 Not Modifiedcarries no content. A successful call toerror_for_status()can still leave this status for the caller to handle.- Trace the body decode and the corresponding cache write. Check whether an invalid new body can leave a new validator paired with the old body.
- Check what happens when the caller lacks a matching entry. Follow the application's error or unconditional refetch path.
2. Keep the payload and validator coherent
- For eligible cached entries, send the saved ETag as
If-None-Matchwithout stripping its quotes or weak prefix. - On a valid 304, reuse the representation selected for revalidation and apply eligible response metadata under the cache's rules. A request without a matching stored entry needs its separate fallback.
- On a 200, capture the returned validator and validate the body before replacing the stored pair. Retain the previous pair on decode failure if the existing cache policy permits it.
- Preserve the caller's status and transport error paths for other responses. A failed refresh and permission to serve stale data are separate decisions.
- Use the real key and metadata policy when reuse depends on URL, representation, user, or request headers.
3. Test requests and stored state together
- Start without a stored entry. Return a valid 200 and observe that the next request sends its validator.
- Return a bodyless 304. Assert that the same JSON payload is available without a decode error.
- Return a malformed 200 with a different ETag. Verify the error and the next request's validator, then return a valid 200 with the replacement validator.
- Validate the new version with a second 304. For production changes, test a missing entry, a non-cacheable response, and concurrent refresh races through the real caller as applicable.
Example
Reuse a validated JSON body runs a reqwest client against a scriptable local endpoint. It reproduces a 304 decode error, then verifies old-body reuse, failure preservation, and successful replacement.
Limitations
- The fixture demonstrates one private entry for a synthetic endpoint. It leaves HTTP freshness calculations,
Vary, shared cache rules, and metadata updates outside the test. - The server is a local HTTP/1.1 fixture. TLS, redirects, intermediaries, and concurrent cache writes need separate validation.
- A retained body is only safe to serve within the application's freshness, privacy, and stale-response policy.
Safety and Side Effects
The example opens one loopback listener on an ephemeral port and sends only synthetic requests to it with proxies disabled. Keep real validators, authorization headers, and private representations out of the fixture.
Related Skills and Documentation
- API Design Principles covers the server-side status and header contract.
- RFC 9110 section 15.4.5 defines 304 response semantics.
- RFC 9111 section 4.3.4 describes updating stored entries after validation.
- reqwest Response documents status and body handling.