Start an HTTP GET request in the background, and report its completion via a deferred.
URL to connect to.
HTTP headers to send.
If not NULL, it must be a string, specifying a file.
The body of the response is written to this file.
Options to set on the handle. Passed to
curl::handle_setopt().
Progress handler function. It is only used if the response body is written to a file. See details below.
Whether and how to retry failed requests. It can be:
TRUE (the default) to retry with the default settings,
FALSE or NULL to never retry,
a single number to set the maximum number of retries, or
a named list to configure retries, with entries (all optional):
limit: maximum number of retries, defaults to 2.
methods: character vector of HTTP methods to retry. Defaults to
the idempotent methods GET, PUT, HEAD, DELETE, OPTIONS,
TRACE and QUERY. In particular POST is not retried by
default.
status_codes: integer vector of HTTP status codes to retry on.
Defaults to 408, 413, 429, 500, 502, 503, 504, 521, 522 and 524.
errors: whether to retry on connection errors, defaults to TRUE.
backoff: a function that takes the (integer) retry attempt number
and returns the number of seconds to wait before the next attempt.
Defaults to an exponential backoff.
Between retries async waits with an exponential backoff, unless the
response has a Retry-After header, which is then honored. Use the
backoff entry to customize the wait.
Deferred object.
An async HTTP deferred object is also an event emitter, see
event_emitter. Use $event_emitter to access the event emitter API,
and call $event_emitter$listen_on() etc. to listen on HTTP events,
etc.
"data" is emitted when we receive data from the server, the data is
passed on to the listeners as a raw vector. Note that zero-length
raw vectors might also happen.
"end" is emitted at the end of the HTTP data stream, without
additional arguments (Also on error.)
Here is an example, that uses the web server from the webfakes package:
http <- webfakes::new_app_process(webfakes::httpbin_app())
stream_http <- function() {
query <- http_get(http$url("/drip?duration=3&numbytes=10"))
query$event_emitter$
listen_on("data", function(bytes) {
writeLines(paste("Got", length(bytes), "byte(s):"))
print(bytes)
})$
listen_on("end", function() {
writeLines("Done.")
})
query
}
response <- synchronise(stream_http())http_get can report on the progress of the download, via the
on_progress argument. This is called with a list, with entries:
url: the specified url to download
handle: the curl handle of the request. This can be queried using
curl::handle_data() to get the response status_code, the final
URL (after redirections), timings, etc.
file: the file argument.
total: total bytes of the response. If this is unknown, it is set
to zero.
current: already received bytes of the response.
Other asyncronous HTTP calls:
http_head(),
http_setopt()
httpbin <- webfakes::new_app_process(webfakes::httpbin_app())
afun <- async(function() {
http_get(httpbin$url("/status/200"))$
then(function(x) x$status_code)
})
synchronise(afun())
#> [1] 200