Skip Unchanged Downloads with ETag Requests
Send If-None-Match on repeat HTTP requests, handle 304 responses, and reuse the stored body when nothing changed.
Code
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
)
var payload = []byte("configuration-v1")
func fetchConditional(
client *http.Client,
url string,
etag string,
) (body []byte, newETag string, notModified bool, err error) {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, "", false, err
}
if etag != "" {
request.Header.Set("If-None-Match", etag)
}
response, err := client.Do(request)
if err != nil {
return nil, "", false, err
}
defer response.Body.Close()
if response.StatusCode == http.StatusNotModified {
return nil, etag, true, nil
}
if response.StatusCode != http.StatusOK {
return nil, "", false, fmt.Errorf("unexpected status %d", response.StatusCode)
}
body, err = io.ReadAll(io.LimitReader(response.Body, 1<<20))
if err != nil {
return nil, "", false, err
}
return body, response.Header.Get("ETag"), false, nil
}
func main() {
requests := 0
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
requests++
writer.Header().Set("ETag", "\"v1\"")
if request.Header.Get("If-None-Match") == "\"v1\"" {
writer.WriteHeader(http.StatusNotModified)
return
}
_, _ = writer.Write(payload)
}))
defer server.Close()
client := server.Client()
body, etag, notModified, err := fetchConditional(client, server.URL, "")
if err != nil || notModified || string(body) != string(payload) || etag == "" {
panic("first request should download the payload")
}
fmt.Printf("first: body=%q etag=%s\n", body, etag)
body, _, notModified, err = fetchConditional(client, server.URL, etag)
if err != nil || !notModified || len(body) != 0 || requests != 2 {
panic("second request should return 304 without a body")
}
fmt.Printf("second: notModified=%v requests=%d\n", notModified, requests)
}
How it works
Skip Unchanged Downloads with ETag Requests is built around the functions fetchConditional and main.
Failures are values here: 5 sites check err and decide instead of letting one propagate. A defer statement pins the cleanup to the function boundary rather than scattering it through the body.
What to watch for
- Errors are ordinary return values; skipping the
errcheck means using a zero value silently.
Applying it
Entry signature: func fetchConditional( client *http.Client, url string, etag string, ) (body []byte, newETag string, notMod…
The demo output is labeled (first: body=, second: notModified=), so each line traces straight back to the statement that printed it.
Execute the file and main walks through fetchConditional, in that order.
Statements that matter
return nil, "", false, fmt.Errorf("unexpected status %d", response.StatusCode)
panic("second request should return 304 without a body")
if err != nil || notModified || string(body) != string(payload) || etag == "" {
panic("first request should download the payload")
}
Interactive Tools for This Topic
FAQ
- Is there exception handling?
- Through `error` values, not panics — the snippet checks `err != nil` at 5 sites.
- What does Skip Unchanged Downloads with ETag Requests print when run?
- Running the file directly is enough — expect labeled output lines such as `first: body=`, `second: notModified=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.