Validate an ETag with curl
An ETag check is a conditional GET: store the ETag from the first response, send it in If-None-Match with curl's --etag-compare or -H, and treat a 304 status as proof that the cached copy is still fresh. A 200 status means the validator no longer matches, so the body must be fetched again.
The short answer
An ETag check is two requests. Fetch the file once and keep the ETag response header. Later, send that value in an If-None-Match request header. A 304 Not Modified response means the representation has not changed and you can reuse the stored copy. A 200 OK response means the validator did not match, so a new body follows.
curl 7.68 and later can store and replay the tag for you with --etag-save and --etag-compare.
A minimal example
URL=https://example.com/data.json
# First request: save the body and the ETag header value
curl -sS --etag-save etag.txt -o data.json "$URL"
# Later: ask the server whether the saved tag still matches
status=$(curl -sS --etag-compare etag.txt -o /dev/null -w '%{http_code}' "$URL")
printf '%s\n' "$status"
A fresh download normally returns 200; an unchanged resource returns 304. The -o /dev/null discards any body, so only the status code reaches the terminal. Check that etag.txt is not empty first: a response without an ETag leaves nothing to compare.
Without the dedicated flags, send the header yourself. Capture the tag from a GET response rather than only from a HEAD request, because some origins generate headers differently per method:
etag=$(curl -sS -D - -o /dev/null "$URL" | awk -F': ' 'tolower($1)=="etag" {print $2}' | tr -d '\r')
[ -n "$etag" ] && curl -sS -H "If-None-Match: $etag" -o /dev/null -w '%{http_code}\n' "$URL"
The header value includes the quotes, for example If-None-Match: "3f80f-1b6-5f6f7a8b". Do not add another pair.
Reading the answer
| Status | Meaning | What to do |
|---|---|---|
304 |
The stored validator matched; no body is sent | Keep the cached body and refresh its stored headers |
200 |
The validator did not match, or the server ignored it | Replace the cached body with this response |
404 or 5xx |
The resource or server failed | Do not treat this as a freshness answer |
Do not infer freshness from the absence of a body. A 304 is defined to have no content, so -o produces an empty file; see why an HTTP 304 response has no body. Conversely, a 200 does not prove the bytes changed: a server may ignore If-None-Match entirely, or emit a different ETag on every response. Validation only works when the ETag is stable for the same representation.
Why the validator can fail to match
An ETag identifies a specific representation, not a URL. If the server negotiates content encoding or language, each variant can carry a different tag. A proxy might strip or rewrite the header, and a misconfigured origin might compute the value from a timestamp. In those cases every conditional request returns 200.
Weak tags need care. An ETag beginning with W/ is a weak validator; If-None-Match uses weak comparison, so a weak tag can still produce a 304. If-Match uses strong comparison, where weak tags never match. The difference is covered in strong and weak ETags.
Edge cases and practical checks
--etag-save overwrites its file on every response, so run the save request before the compare request, not the other way around. --etag-compare only adds the request header; it does not inspect the response for you.
Conditional requests behave the same over HTTP/1.1 and HTTP/2, but HTTP/2 header names are lowercase on the wire. Match header names case-insensitively when parsing, as the awk command above does.
curl exits with status 0 for both 200 and 304. Scripts must branch on the HTTP code, not the process exit code; --fail does not help because 304 is not an error.
A quick manual check is to run the compare twice. A stable resource should return 304 on later attempts:
for i in 1 2; do
curl -sS --etag-compare etag.txt -o /dev/null -w "%{http_code}\n" "$URL"
done
If the first call already returns 304, the saved tag was still current. If both return 200, inspect the response headers for an ETag before assuming the file changed; a server that sends no validator cannot support this workflow.
Sources
Revision history
- Published
- Updated
- — Initial publication.