Built-in behaviours
By default, RestClient caches responses, paces and retries requests, and limits how long a request may take.
Caching
Responses to GET, HEAD and QUERY requests are kept on disk for as long as the server says they stay current, through its Cache-Control or Expires headers. A response the server marks with an ETag or Last-Modified is kept for two weeks past that, and when it's next wanted RestClient asks the server whether it has changed. An unchanged response costs the server a short reply, and is good for another stretch.
- A response is stored under its URL, method, headers and payload, so requests with different keys or
Acceptheaders never share one. Cache-Control: no-storekeeps a response off the disk, andno-cachehas it checked with the server each time it's wanted. A response that says nothing about caching (noCache-Control,Expires,ETagorLast-Modified) isn't kept at all, unlesscachelifetimegives it a lifetime.- It is dropped once it's out of date with nothing to check it against, when a
POST,PUT,PATCHorDELETEsucceeds at the same URL, or when it can't be read as the endpoint's result type. postprocesssees a stored response with its original headers, anAgeheader saying how many seconds old it is, and:storedor:revalidatedas itsResponse'ssource. Its rate-limit headers are as old as it is, which is whyquotaignores stored responses.- Give a config
cache = falseto turn caching off, or definecachelifetimefor an endpoint to choose how long its responses stay current. The cache lives in your user cache folder (on Linux,~/.cache/julia/restclient/), and deleting that folder clears it.
Waiting and retrying
Requests through configs with the same host and key are paced together, since that's how the server counts them. They are sent no faster than the config's RateLimit allows, and they all wait when a response asks for a pause: with a Retry-After header, or with X-RateLimit-Remaining: 0 and an X-RateLimit-Reset time (in seconds from now, as a Unix time, or as a date). nextallowed can ask for longer pauses than the headers do. For an API that counts some endpoints apart, such as GitHub's searches, give those a ratebucket of their own, and they're paced and paused separately.
An unsuccessful response is retried, up to five attempts in all, when:
- it's a 429 (Too Many Requests);
- it's a 403 or 503 whose headers say when to try again;
nextallowedgives a time for it, whatever the method;- it's a 502, 503 or 504 from a gateway, and the method is safe to repeat (not
POSTorPATCH). One whose body reads as the endpoint'serrortypecame from the API itself, and isn't retried.
A retry waits until the time it was given, or failing that for about 0.5, 1, 2 and then 4 seconds. No wait is ever longer than the config's timeout: a retry that would need longer gives you the response as a ResponseError, and a request that would be held back for longer raises a RateLimitError rather than being sent. With the default timeout = Inf, a wait can last until an hourly quota renews, so give a timeout if you'd rather be told.
quota gives the rate limit the server last reported, for showing a user how many requests they have left.
Time limits
A config's timeout bounds how long a request may take as a whole, and for a Stream, how long it may go without new data. Without one, two limits still stop a request (other than a stream) that will never finish:
- the response must begin within two minutes, since a server may think for a while before answering;
- once it has begun, it must not stall for more than 20 seconds.
A longer timeout raises both. A request that hits either raises a TransportError saying which.
What's sent
Every request says User-Agent: RestClient.jl/<version>, with an Accept header for the result's format and a Content-Type for the payload's. Headers you give, through the endpoint's or its API's headers, are added to these, and replace them when they share a name. RestClient also asks for compressed responses and decompresses them, and follows up to 50 redirects. A 303 (See Other) turns the request into a GET without its body; every other redirect, 301 and 302 included, sends the same method and body on.
Keys are hidden as ***** in errors, debug output, and the cache.
Order of events
validatechecks the endpoint, before anything is sent.- The request waits its turn, then is sent, or served from the cache.
nextallowedsees each response from the server, and retries happen.- A successful response is read as the endpoint's
responsetype; one that can't be is tried as itserrortype, so an error inside a 200 is raised as one. An unsuccessful response is read as theerrortypeand raised as aResponseError. postprocesssees what was read, even when there is no result type (asnothing), and its result is unwrapped bycontentsor wrapped in aList. A value of theerrortypeit throws is raised as aResponseError.
A HEAD request's result is whether the resource exists: true, or false for a 404 or 410.