Capturing 4xx response bodies with httr2::req_perform_stream

Hello,

The httr2 package supports capturing the bodies of 4xx responses and including the in the R error message using the req_error method:

library(httr2)
req <- ...
req <- req_error(req, body = \(resp) resp_body_string(resp)
resp <- req_perform(req)

However, error responses are not handled and converted to R errors in the same way using req_perform_stream -- a response object with an error status code but no body is returned.
Moreover, the response body isn't included in the returned response object,
and so the error message cannot be extract manually. This is true even when setting is_error to a lambda returning FALSE, which in the req_perform successfully turns off the http-to-R error handling and yields a 4xx response with a body. The following obfuscated example uses a private API and so is not a reprex:

> httr2::req_perform(request)
<httr2_response>
POST http://obfuscated
Status: 400 Bad Request
Content-Type: text/plain
Body: In memory (147 bytes)
Browse[3]> httr2::req_perform_stream(request, cb, buffer_kb = buffer_kb)
<httr2_response>
POST http://obfuscated
Status: 400 Bad Request
Content-Type: text/plain
Body: None

My current workaround is to repeat the request using req_perform when req_perform_stream returns an error status, but this can mean repeating large POST requests. Is there a way to get the 400 response's along with the single req_perform_stream request?

This difference in behavior makes me wonder whether it's intentional, so I would suggest posting an issue on github:

and sharing the issue link here.

1 Like

@dromano Thanks for the suggestion -- I have created issue

1 Like