• req_body_file() sends a local file.

  • req_body_raw() sends a string or raw vector.

  • req_body_json() sends JSON encoded data.

  • req_body_form() sends form encoded data.

  • req_body_multipart() creates a multi-part body.

Adding a body to a request will automatically switch the method to POST.

req_body_raw(req, body, type = NULL)

req_body_file(req, path, type = NULL)

req_body_json(req, data, auto_unbox = TRUE, digits = 22, null = "null", ...)

req_body_form(req, data)

req_body_multipart(req, data)

Arguments

req

A request.

body

A literal string or raw vector to send as body.

type

Content type. For req_body_file(), the default will will attempt to guess from the extension of path.

path

Path to file to upload.

data

Data to include in body. For req_body_json() this can be any R data structure that can be serialised to JSON, for req_body_form() it should be a named list of simple values, and req_body_multipart() it should be a named list containing strings or objects produced by curl::form_file()/curl::form_data().

auto_unbox

Should length-1 vectors be automatically "unboxed" to JSON scalars?

digits

How many digits of precision should numbers use in JSON?

null

Should NULL be translated to JSON's null ("null") or an empty list ("list").

...

Other arguments passed on to jsonlite::toJSON().

Value

A modified HTTP request.

Examples

req <- request("http://httpbin.org/post")

# Most APIs expect small amounts of data in either form or json encoded:
req %>%
  req_body_form(list(x = "A simple text string")) %>%
  req_dry_run()
#> POST /post HTTP/1.1
#> Host: httpbin.org
#> User-Agent: httr2/0.1.1.9000 r-curl/4.3.2 libcurl/7.68.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip, br
#> Content-Type: application/x-www-form-urlencoded
#> Content-Length: 28
#> 
#> x=A%20simple%20text%20string

req %>%
  req_body_json(list(x = "A simple text string")) %>%
  req_dry_run()
#> POST /post HTTP/1.1
#> Host: httpbin.org
#> User-Agent: httr2/0.1.1.9000 r-curl/4.3.2 libcurl/7.68.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip, br
#> Content-Type: application/json
#> Content-Length: 28
#> 
#> {"x":"A simple text string"}

# For total control over the body, send a string or raw vector
req %>%
  req_body_raw("A simple text string") %>%
  req_dry_run()
#> POST /post HTTP/1.1
#> Host: httpbin.org
#> User-Agent: httr2/0.1.1.9000 r-curl/4.3.2 libcurl/7.68.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip, br
#> Content-Length: 20
#> 
#> A simple text string

# There are two main ways that APIs expect entire files
path <- tempfile()
writeLines(letters[1:6], path)

# You can send a single file as the body:
req %>%
  req_body_file(path) %>%
  req_dry_run()
#> POST /post HTTP/1.1
#> Host: httpbin.org
#> User-Agent: httr2/0.1.1.9000 r-curl/4.3.2 libcurl/7.68.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip, br
#> Content-Length: 12
#> 
#> a
#> b
#> c
#> d
#> e
#> f

# You can send multiple files, or a mix of files and data
# with multipart encoding
req %>%
  req_body_multipart(list(a = curl::form_file(path), b = "some data")) %>%
  req_dry_run()
#> POST /post HTTP/1.1
#> Host: httpbin.org
#> User-Agent: httr2/0.1.1.9000 r-curl/4.3.2 libcurl/7.68.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip, br
#> Content-Length: 316
#> Content-Type: multipart/form-data; boundary=------------------------2a5b76d3d55111b5
#> 
#> --------------------------2a5b76d3d55111b5
#> Content-Disposition: form-data; name="a"; filename="file2f105035c971"
#> Content-Type: application/octet-stream
#> 
#> a
#> b
#> c
#> d
#> e
#> f
#> 
#> --------------------------2a5b76d3d55111b5
#> Content-Disposition: form-data; name="b"
#> 
#> some data
#> --------------------------2a5b76d3d55111b5--