url: A character string naming the URL of a resource to be downloaded.
handle: A curl handle object.
path: Path to save results
fun: Callback function. Should have one argument, which will be a raw vector.
done: callback function for completed request. Single argument with response data in same structure as curl_fetch_memory .
fail: callback function called on failed request. Argument contains error message.
pool: a multi handle created by new_pool . Default uses a global pool.
data: (advanced) callback function, file path, or connection object for writing incoming data. This callback should only be used for streaming applications, where small pieces of incoming data get written before the request has completed. The signature for the callback function is write(data, final = FALSE). If set to NULL the entire response gets buffered internally and returned by in the done callback (which is usually what you want).
Details
The curl_fetch_*() functions automatically raise an error upon protocol problems (network, disk, TLS, etc.) but do not implement application logic. For example, you need to check the status code of HTTP requests in the response by yourself, and deal with it accordingly.
Both curl_fetch_memory() and curl_fetch_disk have a blocking and a non-blocking C implementation. The latter is slightly slower but allows for interrupting the download prematurely (using e.g. CTRL+C or ESC). Interrupting is enabled when R runs in interactive mode or when getOption("curl_interrupt") == TRUE.
The curl_fetch_multi() function is the asynchronous equivalent of curl_fetch_memory(). It wraps multi_add() to schedule requests which are executed concurrently when calling multi_run()`` . For each successful request, the done callback is triggered with response data. For failed requests (when curl_fetch_memory() would raise an error), the fail function is triggered with the error message.
Examples
# Load in memoryres <- curl_fetch_memory("https://hb.cran.dev/cookies/set?foo=123&bar=ftw")res$content
# Save to diskres <- curl_fetch_disk("https://hb.cran.dev/stream/10", tempfile())res$content
readLines(res$content)# Stream with callbackdrip_url <-"https://hb.cran.dev/drip?duration=3&numbytes=15&code=200"res <- curl_fetch_stream(drip_url,function(x){ cat(rawToChar(x))})# Async APIdata <- list()success <-function(res){ cat("Request done! Status:", res$status,"\n") data <<- c(data, list(res))}failure <-function(msg){ cat("Oh noes! Request failed!", msg,"\n")}curl_fetch_multi("https://hb.cran.dev/get", success, failure)curl_fetch_multi("https://hb.cran.dev/status/418", success, failure)curl_fetch_multi("https://urldoesnotexist.xyz", success, failure)multi_run()str(data)