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

Can't find how to read body in chunks #998

Open
CharlieBytesX opened this issue Nov 28, 2024 · 1 comment
Open

Can't find how to read body in chunks #998

CharlieBytesX opened this issue Nov 28, 2024 · 1 comment

Comments

@CharlieBytesX
Copy link

Hi, I'm building an api gateway.
And inside my proxy method i have this code section. I want to know what is the right way of reading and sending in chunks instead of load all the body in memory. Could you help me, I wasn't able to find a method to load the body in chunks

    let client = reqwest::Client::new();

    let mut request_to_destination_server_builder = client
        .request(req.method().clone(), &destination_url)
        .query(req.queries())
        .headers(req.headers().clone());

    //  If request  have content length put that body in the request to
    //  destination server
    if req.headers().get("Content-Length").is_some() {
        let payload = match req.payload_with_max_size(MAX_SIZE_BODY).await {
            //Cloning the req.payload() is ok because it just clones the Bytes object and not the internal
            //buffer (buffer is where all the bytes of the body are stored)
            Ok(v) => v.clone(),
            Err(e) => {
                eprintln!("{}", e);
                res.status_code(StatusCode::BAD_REQUEST);
                let message =
                    "GW: Content-length header set but couldn't extract request body".to_string();
                res.render(Text::Plain(&message));
                info_for_open_observe.status_code = StatusCode::BAD_REQUEST.as_u16();
                info_for_open_observe.error = super::tracking::ErrorInfo {
                    is_client_error: false,
                    is_host_server_error: false,
                    is_gateway_error: true,
                    message,
                };
                info_for_open_observe.timing = super::tracking::TimingInfo {
                    total_time_in_gateway: start_time.elapsed().as_millis(),
                    total_time_host_server_answer_in_ms: None,
                };
                return crate::gateway::tracking::ProxyResultVariants::SendToOpenObserve(
                    info_for_open_observe,
                );
            }
        };
        request_to_destination_server_builder =
            request_to_destination_server_builder.body(payload.clone());
    }

    //  NOTE: Initialize struct to store durations
@chrislearn
Copy link
Member

Try to take Request's body and give it to request_to_destination_server_builder.

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