I'm trying to fetch the full error responses from a Qualtrics API response with httr2, however, the error-catching from httr2 itself fails because the error response body is not what httr2 expects.
My problem is twofold:
- When running the API call, it errors (rightly so), but doesn't create an R object from where I can fetch the error information.
- I solved this problem by adding a
req_error(is_error = \(resp) FALSE)
code snippet. However, this just returns the correct http status code, but with a weird/wrong status description.
My code:
library(httr2)
req_get_users <- request(";) |>
req_method("POST") |>
req_oauth_client_credentials(client = oauth_client(id = "my_id",
secret = "my_secret",
token_url = ";),
scope = "manage:all",
token_params = list("grant_type" = "client_credentials")) |>
req_perform()
This request rightly errors (because the API only accepts a GET request, not a POST request).
What I want to achieve is that a) the API call would still return a response object and b) that I'm able to retrieve the full error message from that body.
I'm able to get a response object by adding the abovementioned req_error
code snippet.
The problem is that the response body doesn't seem to be a json response body. So resp_body_json(req_get_users)
fails with an error:
Error in `resp_body_json()`:
! Unexpected content type "text/html".
• Expecting type "application/json" or suffix "json".
Run `rlang::last_trace()` to see where the error occurred.
And indeed, if I change the resp_body_json
to resp_body_raw(req_get_users) |> rawToChar()
I'm able to get the error response, but all of this looks quite hack.
Does a general approach in {httr2}
exists that always returns the error test without me having to worry about the object type etc?