admin管理员组文章数量:1377554
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?
本文标签: rError handling fails with httr2 because error object is not a json bodyStack Overflow
版权声明:本文标题:r - Error handling fails with httr2 because error object is not a json body - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744423135a2605554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论