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

Try the httr package in your browser

Any scripts or data that you put into this service are public.

httr documentation built on Aug. 15, 2023, 9:08 a.m.