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

Is the reqwest-response example outdated? #2767

Open
frederikhors opened this issue Jun 4, 2024 · 4 comments
Open

Is the reqwest-response example outdated? #2767

frederikhors opened this issue Jun 4, 2024 · 4 comments
Labels
A-examples E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue.

Comments

@frederikhors
Copy link

Is the example here outdated now that http1 porting is done, right?

https://github.com/tokio-rs/axum/blob/main/examples/reqwest-response/src/main.rs

@jplatte
Copy link
Member

jplatte commented Jun 4, 2024

Hm, it's already upgraded to reqwest 0.12, but it looks more complicated than necessary. I'm not sure actually. If you find a way to cut down on conversion boilerplate (might be simple, or not), a PR would be appreciated.

@frederikhors
Copy link
Author

I'll PR as soon I understand how. We're trying to find a way here: https://users.rust-lang.org/t/a-proxy-with-axum-0-7-and-reqwest-0-12-based-on-http-1/112489/2.

@frederikhors
Copy link
Author

I found two ways:

  1. This is one way, from https://users.rust-lang.org/t/a-proxy-with-axum-0-7-and-reqwest-0-12-based-on-http-1/112489/5:

    use http_body_util::BodyExt;
    
    let http_client = reqwest::Client::new();
    
    let (headers, body) = req.into_parts();
    
    let req = reqwest::Request::try_from(axum::extract::Request::from_parts(
        headers,
        body.collect().await.unwrap().to_bytes(),
    ))
    .unwrap();
    
    let res = http_client.execute(req).await.unwrap();
  2. This is another way, from https://stackoverflow.com/a/78577792:

    use sync_wrapper::SyncStream;
    
    let http_client = reqwest::Client::new();
    
    let req = req.map(|body| reqwest::Body::wrap_stream(SyncStream::new(body.into_data_stream())));
    
    let req = reqwest::Request::try_from(req).unwrap();
    
    let res = http_client.execute(req).await.unwrap();

I think I'll go with SyncStream.

@jplatte
Copy link
Member

jplatte commented Jun 6, 2024

In case you're waiting for a reply, yes that sounds like a good plan :)

The first snippet would buffer the entire request body in memory, which can be a big problem for large requests.

@jplatte jplatte added E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. A-examples labels Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-examples E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue.
Projects
None yet
Development

No branches or pull requests

2 participants