File: upload.R

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#' upload file
#'
#' @export
#' @param path (character) a single path, file must exist
#' @param type (character) a file type, guessed by [mime::guess_type] if
#' not given
#' @examples \dontrun{
#' # image
#' path <- file.path(Sys.getenv("R_DOC_DIR"), "html/logo.jpg")
#' (x <- HttpClient$new(url = "https://eu.httpbin.org"))
#' res <- x$post(path = "post", body = list(y = upload(path)))
#' res$content
#'
#' # text file, in a list
#' file <- upload(system.file("CITATION"))
#' res <- x$post(path = "post", body = list(y = file))
#' jsonlite::fromJSON(res$parse("UTF-8"))
#'
#' # text file, as data
#' res <- x$post(path = "post", body = file)
#' jsonlite::fromJSON(res$parse("UTF-8"))
#' }
upload <- function(path, type = NULL) {
  stopifnot(is.character(path), length(path) == 1, file.exists(path))
  if (is.null(type)) type <- mime::guess_type(path)
  curl::form_file(path, type)
}