Skip to content

Conversation

@cgiachalis
Copy link
Contributor

closes #782

library(plumber2)
library(httr2)

api <-  api() |>
  api_get(
    "/health",
    function() {
      plumber2::abort_bad_request("bad request")
    }
  )

mirai::daemons(1)

# run api in a separate process & do not block the current process
mi <- mirai::mirai({
  api$ignite(block = TRUE, showcase = FALSE)
}, api = api)


request("http://127.0.0.1:8080/health") |>  req_perform()

# Error in `req_perform()`:
# ! HTTP 400 Bad Request.
 last_response_json()
{
  "type": "https://datatracker.ietf.org/doc/html/rfc9110#section-15.5.1",
  "title": "Bad Request",
  "status": 400,
  "detail": "bad request"
}

@hadley
Copy link
Member

hadley commented Jan 14, 2026

I'm pretty sure there's some existing suffix parsing code that you could use to determine if there's a +json suffix. (I might be misremembering, but I think it's more generic than just application/problem)

@cgiachalis
Copy link
Contributor Author

cgiachalis commented Jan 14, 2026

Do you want me to replace

httr2/R/last.R

Lines 64 to 66 in c7d4a86

if (!identical(resp_content_type(resp), "application/json")) {
cli::cli_abort("Last response doesn't have a JSON body.")
}

with

  resp_check_content_type(
    resp,
    valid_types = "application/json",
    valid_suffix = "json",
    check_type = TRUE
  )

# or 

check_content_type(
    resp_content_type(resp),
    valid_types = "application/json",
    valid_suffix = "json",
    inform_check_type = TRUE
)

?

Example:

library(httr2)

req_perform(request(httr2:::example_url("/xml")))
#> <httr2_response>
#> GET http://127.0.0.1:57838/xml
#> Status: 200 OK
#> Content-Type: application/xml
#> Body: In memory (610 bytes)
resp <- last_response()

# last_response_json() will use
httr2:::check_content_type(
  resp_content_type(resp),
  valid_types = "application/json",
  valid_suffix = "json",
  inform_check_type = TRUE
)
#> Error:
#> ! Unexpected content type "application/xml".
#> • Expecting type "application/json" or suffix "json".

Created on 2026-01-14 with reprex v2.1.1

@hadley
Copy link
Member

hadley commented Jan 14, 2026

Yes, that sounds great

@hadley
Copy link
Member

hadley commented Jan 14, 2026

Looks good! Can you please also add a news bullet to the top of NEWS.md? It should briefly describe the change and end with (@yourname, #issuenumber).

@hadley hadley merged commit de347bd into r-lib:main Jan 14, 2026
@cgiachalis cgiachalis deleted the fix-782 branch January 14, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should last_response_json() work with 'application/problem+json' content type?

2 participants