Curl No Cache Example

Curl keeps no cache of its own, so a stale response always comes from something in between: a CDN, a reverse proxy, a corporate gateway. The fix is a request header that asks those caches to check with the origin.

Last updated . How this page is checked.

The command

curl -H "Cache-Control: no-cache" -H "Pragma: no-cache" https://httpbin.org/cache

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-H "Cache-Control: no-cache"Asks caches to revalidate with the origin before answering.
-H "Pragma: no-cache"The HTTP/1.0 equivalent, still honoured by some old proxies.
-IShow the response headers that reveal whether a cache answered.
--etag-save / --etag-compareStore an ETag and send it back as If-None-Match (curl 7.68+).

Did a cache answer?

Look at the response headers. Age is the number of seconds the response has been sitting in a cache; its presence means you did not get a fresh copy. CDNs add their own markers, commonly X-Cache: HIT or MISS, or a vendor-specific equivalent. Compare a normal request with a no-cache one and watch those values change.

curl -sI https://example.com/app.js | grep -i -E "^(age|x-cache|cache-control|etag)"
curl -sI -H "Cache-Control: no-cache" https://example.com/app.js | grep -i -E "^(age|x-cache)"

When the header is ignored

A request header is a request. Many CDNs are configured to disregard client Cache-Control, precisely so visitors cannot force traffic to the origin. Two things still work. A unique query string makes a URL the cache has never seen, as long as the cache key includes the query. Purging through the CDN's own API or dashboard is the reliable option.

curl "https://example.com/app.js?nocache=$(date +%s)"

Conditional requests

The opposite goal, downloading only when something changed, uses validators. Send the ETag from last time as If-None-Match and an unchanged resource answers 304 Not Modified with no body. Curl automates the bookkeeping with two flags.

curl --etag-save etag.txt --etag-compare etag.txt -o data.json https://example.com/data.json

Common mistakes

  • no-cache does not mean "do not store". It means "revalidate before use". The directive that forbids storing is no-store, and it belongs on responses.
  • These headers do nothing to your browser's cache. They only affect the request curl is making.
  • Cache-busting query strings fail on CDNs set to ignore the query when building the cache key.

Related curl examples

More under Headers

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I disable caching in a curl request?

Add -H "Cache-Control: no-cache" and, for old proxies, -H "Pragma: no-cache". Curl itself does not cache, so this targets CDNs and proxies between you and the server.

Does curl cache responses?

No. Curl has no response cache. A stale result comes from an intermediate cache such as a CDN or proxy.

How can I tell whether a response came from a cache?

Inspect the response headers with curl -I. An Age header or a marker such as X-Cache: HIT indicates a cached copy.