diff --git a/NEWS.md b/NEWS.md index e2e85e4c..db15ae62 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # httr2 (development version) +* `last_response_json()` now works with content-types that end with `+json`, +e.g., `application/problem+json` (@cgiachalis, #782). + # httr2 1.2.2 * httr2 will now emit OpenTelemetry traces for all requests when tracing is enabled. Requires the `otelsdk` package (@atheriel, #729). diff --git a/R/last.R b/R/last.R index 625d5d01..c57beb14 100644 --- a/R/last.R +++ b/R/last.R @@ -61,9 +61,12 @@ last_response_json <- function(pretty = TRUE) { if (is.null(resp)) { cli::cli_abort("No request has been made successfully yet.") } - if (!identical(resp_content_type(resp), "application/json")) { - cli::cli_abort("Last response doesn't have a JSON body.") - } + check_content_type( + resp_content_type(resp), + valid_types = "application/json", + valid_suffix = "json", + inform_check_type = TRUE + ) httr2_json(resp_body_string(resp), pretty = pretty) } diff --git a/tests/testthat/_snaps/last.md b/tests/testthat/_snaps/last.md index a8da3918..90957b31 100644 --- a/tests/testthat/_snaps/last.md +++ b/tests/testthat/_snaps/last.md @@ -35,7 +35,8 @@ last_response_json() Condition Error in `last_response_json()`: - ! Last response doesn't have a JSON body. + ! Unexpected content type "application/xml". + * Expecting type "application/json" or suffix "json". # useful errors if no last request/response