Skip to content

Cannot capture 4xx response bodies with req_perform_stream #479

@jwimberl

Description

@jwimberl

I'm finding a difference in behavior between req_perform and req_perform_stream when it comes to capturing HTTP errors and translating them to R errors, and moreover in capturing 4xx response bodies. Both occur straightforwardly with req_perform, following the documented instructions:

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

This will throw an R error that includes the text body from the error message.

However, error responses are not handled and converted to R errors in the same way using req_perform_stream:

> req <- ...
> req <- req_error(req, body = \(resp) resp_body_string(resp)
> req_perform(req)
<httr2_response>
POST http://obfuscated
Status: 400 Bad Request
Content-Type: text/plain
Body: None

This returns a response object with an error status code is returned, but does not throw an R error. It also does not include any body. This is true even when setting is_error to a lambda returning FALSE. In the req_perform case, that successfully turns off the http-to-R error handling and yields a 4xx response with a body, but for req_perform_stream the response body is still empty. The following obfuscated example uses a private API and so is not a reprex:

> request <- ...
> request <- httr2::req_error(request, is_error = \(z) FALSE)
> httr2::req_perform(request)
<httr2_response>
POST http://obfuscated
Status: 400 Bad Request
Content-Type: text/plain
Body: In memory (147 bytes)

> 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions