Skip to contents

Asserts that a response object is of a specific MIME type. Convenience wrapper around httr::http_type().

Usage

assert_mime_type(
  response,
  mime_type,
  msg = paste0("The response's MIME type is {.val {mime_type_actual}} ",
    "but expected was {.val {mime_type}}."),
  msg_suffix = ""
)

Arguments

response

A response object.

mime_type

Expected MIME type, e.g. "application/json". A character scalar.

msg

Message to display in case of an error. Supports cli's inline markup. A character scalar.

msg_suffix

Additional string to append to msg. Supports cli's inline markup.

Value

response, invisibly.

See also

Other HTTP functions: is_http_success(), is_url(), req_cached()

Other functions extending the checkmate package: assert_class_any(), assert_cli(), assert_df_or_tibble(), assert_inf_count(), assert_pkg()

Examples

httr::GET("https://api.github.com/users/salim-b") |> pal::assert_mime_type("application/json")

# an informative error is thrown when the assertion is violated
try(
  httr::GET("https://api.github.com/users/salim-b") |> pal::assert_mime_type("text/plain")
)
#> Error in pal::assert_mime_type(httr::GET("https://api.github.com/users/salim-b"),  : 
#>   The response's MIME type is "application/json" but expected was "text/plain".