diff --git a/test-programs/src/bin/client_post_with_body.rs b/test-programs/src/bin/client_post_with_body.rs index 2cbb3eb..797fc7d 100644 --- a/test-programs/src/bin/client_post_with_body.rs +++ b/test-programs/src/bin/client_post_with_body.rs @@ -8,15 +8,20 @@ struct Data { } fn main() { - let buffer = [0; 5000]; + // Make sure the final body is larger than 1024*1024, but we cannot allocate + // so much memory directly in the wasm program, so we use the `repeat` + // method to increase the body size. + const LEN: usize = 1024; + const REPEAT: usize = 1025; + let buffer = [0; LEN]; let resp = Client::new() .post("https://httpbin.org/post") - .body(buffer) + .body(buffer.repeat(REPEAT)) .connect_timeout(Duration::from_secs(5)) .send() .unwrap(); assert_eq!(resp.status_code(), 200); let data = resp.json::().unwrap(); - assert_eq!(data.data.len(), 5000); + assert_eq!(data.data.len(), LEN * REPEAT); } diff --git a/waki/src/request.rs b/waki/src/request.rs index eb57bb6..64ec6fa 100644 --- a/waki/src/request.rs +++ b/waki/src/request.rs @@ -232,19 +232,20 @@ impl Request { .map_err(|()| anyhow!("failed to set path_with_query"))?; } + let outgoing_body = req + .body() + .map_err(|_| anyhow!("outgoing request write failed"))?; + let options = RequestOptions::new(); options .set_connect_timeout(self.connect_timeout) .map_err(|()| anyhow!("failed to set connect_timeout"))?; + let future_response = outgoing_handler::handle(req, Some(options))?; - let outgoing_body = req - .body() - .map_err(|_| anyhow!("outgoing request write failed"))?; let body = self.body.bytes()?; write_to_outgoing_body(&outgoing_body, body.as_slice())?; OutgoingBody::finish(outgoing_body, None)?; - let future_response = outgoing_handler::handle(req, Some(options))?; let incoming_response = match future_response.get() { Some(result) => result.map_err(|()| anyhow!("response already taken"))?, None => {