req <- request(example_url()) |>
req_url_path("/post")
# Most APIs expect small amounts of data in either form or json encoded:
req |>
req_body_form(x = "A simple text string") |>
req_dry_run()
req |>
req_body_json(list(x = "A simple text string")) |>
req_dry_run()
# For total control over the body, send a string or raw vector
req |>
req_body_raw("A simple text string") |>
req_dry_run()
# 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()
# You can send multiple files, or a mix of files and data
# with multipart encoding
req |>
req_body_multipart(a = curl::form_file(path), b = "some data") |>
req_dry_run()
Run the code above in your browser using DataLab