http_post: Asynchronous HTTP POST request

View source: R/http.R

http_postR Documentation

Asynchronous HTTP POST request

Description

Start an HTTP POST request in the background, and report its completion via a deferred value.

Usage

http_post(
  url,
  data = NULL,
  data_file = NULL,
  data_form = NULL,
  headers = character(),
  file = NULL,
  options = list(),
  on_progress = NULL
)

Arguments

url

URL to connect to.

data

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

Data file to send. At most one of data, data_file and data_form can be non NULL.

data_form

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.

headers

HTTP headers to send.

file

If not NULL, it must be a string, specifying a file. The body of the response is written to this file.

options

Options to set on the handle. Passed to curl::handle_setopt().

on_progress

Progress handler function. It is only used if the response body is written to a file. See details at http_get().

Details

An async HTTP deferred object is also an event emitter, see http_get() for details, and also event_emitter.

Examples

json <- jsonlite::toJSON(list(baz = 100, foo = "bar"))

do <- function() {
  headers <- c("content-type" = "application/json")
  http_post("https://eu.httpbin.org/post", data = json, headers = headers)$
    then(http_stop_for_status)$
    then(function(x) {
      jsonlite::fromJSON(rawToChar(x$content))$json
    })
}

synchronise(do())

r-lib/async documentation built on Nov. 19, 2024, 12:08 p.m.