Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stream response body #682

Closed
jgrund opened this issue Oct 17, 2019 · 1 comment
Closed

How to stream response body #682

jgrund opened this issue Oct 17, 2019 · 1 comment

Comments

@jgrund
Copy link

jgrund commented Oct 17, 2019

What's the best way to stream a response body using 0.10.0-alpha?

I know there is access to the chunks of a response with https://docs.rs/reqwest/0.10.0-alpha.1/reqwest/struct.Response.html#method.chunk but I'd like to access the Decoder to use the Stream trait.

Previously, this could be done using into_body, but there does not appear to be an equivalent.

@seanmonstar
Copy link
Owner

For now, I'm reluctant to expose a Stream interface. But it's fairly straight-forward to create one using the async fn chunk with unfold:

let stream = futures::stream::unfold(resp, |mut resp| async move {
    let result = resp.chunk().await;
    match result {
        Ok(Some(chunk)) => Some((Ok(chunk), resp)),
        Ok(None) => None,
        Err(err) => Some((Err(err), resp)),
    }
});

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

No branches or pull requests

2 participants