Start an HTTP POST request in the background, and report its completion via a deferred value.
URL to connect to.
Data to send. Either a raw vector, or a character string
that will be converted to raw with base::charToRaw. At most one of
data, data_file and data_form can be non NULL.
Data file to send. At most one of data, data_file
and data_form can be non NULL.
Form data to send. A name list, where each element
is created with either curl::form_data() or curl::form_file().
At most one of data, data_file and data_form can be non NULL.
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 at http_get().
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.
An async HTTP deferred object is also an event emitter, see
http_get() for details, and also event_emitter.
httpbin <- webfakes::new_app_process(webfakes::httpbin_app())
json <- jsonlite::toJSON(list(baz = 100, foo = "bar"))
do <- function() {
headers <- c("content-type" = "application/json")
http_post(httpbin$url("/post"), data = json, headers = headers)$
then(http_stop_for_status)$
then(function(x) {
jsonlite::fromJSON(rawToChar(x$content))$json
})
}
synchronise(do())
#> $baz
#> [1] 100
#>
#> $foo
#> [1] "bar"
#>