R/response-type.R

Defines functions http_type

Documented in http_type

#' Extract the content type of a response
#'
#' @param x A response
#' @return A string giving the complete mime type, with all parameters
#'   stripped off.
#' @export
#' @examples
#' \dontrun{
#' r1 <- GET("http://httpbin.org/image/png")
#' http_type(r1)
#' headers(r1)[["Content-Type"]]
#'
#' r2 <- GET("http://httpbin.org/ip")
#' http_type(r2)
#' headers(r2)[["Content-Type"]]
#' }
http_type <- function(x) {
  stopifnot(is.response(x))

  type <- x$headers[["Content-Type"]] %||%
    mime::guess_type(x$url, empty = "application/octet-stream")

  parse_media(type)$complete
}

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.