R/upload-file.r

Defines functions as.character.form_file upload_file

Documented in upload_file

#' Upload a file with [POST()] or [PUT()].
#'
#' @param path path to file
#' @param type mime type of path. If not supplied, will be guess by
#'   [mime::guess_type()] when needed.
#' @export
#' @examples
#' citation <- upload_file(system.file("CITATION"))
#' \dontrun{
#' POST("http://httpbin.org/post", body = citation)
#' POST("http://httpbin.org/post", body = list(y = citation))
#' }
upload_file <- 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)
}

#' @export
as.character.form_file <- function(x, ...) x
r-lib/httr documentation built on Nov. 5, 2023, 7:26 a.m.