Skip to content

Commit de347bd

Browse files
authored
Use check_content_type () in last_response_json() (#832)
Adding support for mime-types with json suffix. Fixes #782.
1 parent c7d4a86 commit de347bd

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# httr2 (development version)
22

3+
* `last_response_json()` now works with content-types that end with `+json`,
4+
e.g., `application/problem+json` (@cgiachalis, #782).
5+
36
# httr2 1.2.2
47

58
* httr2 will now emit OpenTelemetry traces for all requests when tracing is enabled. Requires the `otelsdk` package (@atheriel, #729).

R/last.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ last_response_json <- function(pretty = TRUE) {
6161
if (is.null(resp)) {
6262
cli::cli_abort("No request has been made successfully yet.")
6363
}
64-
if (!identical(resp_content_type(resp), "application/json")) {
65-
cli::cli_abort("Last response doesn't have a JSON body.")
66-
}
64+
check_content_type(
65+
resp_content_type(resp),
66+
valid_types = "application/json",
67+
valid_suffix = "json",
68+
inform_check_type = TRUE
69+
)
6770
httr2_json(resp_body_string(resp), pretty = pretty)
6871
}
6972

tests/testthat/_snaps/last.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
last_response_json()
3636
Condition
3737
Error in `last_response_json()`:
38-
! Last response doesn't have a JSON body.
38+
! Unexpected content type "application/xml".
39+
* Expecting type "application/json" or suffix "json".
3940

4041
# useful errors if no last request/response
4142

0 commit comments

Comments
 (0)