diff --git a/server_fn/src/codec/cbor.rs b/server_fn/src/codec/cbor.rs index 051bc266c..a5a91c811 100644 --- a/server_fn/src/codec/cbor.rs +++ b/server_fn/src/codec/cbor.rs @@ -29,7 +29,12 @@ where let mut buffer: Vec = Vec::new(); ciborium::ser::into_writer(&self, &mut buffer) .map_err(|e| ServerFnError::Serialization(e.to_string()))?; - Request::try_new_post_bytes(path, accepts, Cbor::CONTENT_TYPE, &buffer) + Request::try_new_post_bytes( + path, + accepts, + Cbor::CONTENT_TYPE, + Bytes::from(buffer), + ) } } diff --git a/server_fn/src/codec/rkyv.rs b/server_fn/src/codec/rkyv.rs index ab5ed08ae..2d9753ad1 100644 --- a/server_fn/src/codec/rkyv.rs +++ b/server_fn/src/codec/rkyv.rs @@ -35,12 +35,8 @@ where ) -> Result> { let encoded = rkyv::to_bytes::(&self) .map_err(|e| ServerFnError::Serialization(e.to_string()))?; - Request::try_new_post_bytes( - path, - accepts, - Rkyv::CONTENT_TYPE, - encoded.as_ref(), - ) + let bytes = Bytes::copy_from_slice(encoded.as_ref()); + Request::try_new_post_bytes(path, accepts, Rkyv::CONTENT_TYPE, bytes) } } @@ -54,7 +50,7 @@ where { async fn from_req(req: Request) -> Result> { let body_bytes = req.try_into_bytes().await?; - rkyv::from_bytes::(&body_bytes) + rkyv::from_bytes::(body_bytes.as_ref()) .map_err(|e| ServerFnError::Args(e.to_string())) } } diff --git a/server_fn/src/request/browser.rs b/server_fn/src/request/browser.rs index 5708ed3d2..820ea77ff 100644 --- a/server_fn/src/request/browser.rs +++ b/server_fn/src/request/browser.rs @@ -78,12 +78,13 @@ impl ClientReq for BrowserRequest { path: &str, accepts: &str, content_type: &str, - body: &[u8], + body: Bytes, ) -> Result> { let server_url = get_server_url(); let mut url = String::with_capacity(server_url.len() + path.len()); url.push_str(server_url); url.push_str(path); + let body: &[u8] = &body; let body = Uint8Array::from(body).buffer(); Ok(Self(SendWrapper::new( Request::post(&url) diff --git a/server_fn/src/request/mod.rs b/server_fn/src/request/mod.rs index b9ff889d4..785df34a5 100644 --- a/server_fn/src/request/mod.rs +++ b/server_fn/src/request/mod.rs @@ -45,7 +45,7 @@ where path: &str, content_type: &str, accepts: &str, - body: &[u8], + body: Bytes, ) -> Result>; /// Attempts to construct a new `POST` request with form data as the body. diff --git a/server_fn/src/request/reqwest.rs b/server_fn/src/request/reqwest.rs index db20f2873..012985470 100644 --- a/server_fn/src/request/reqwest.rs +++ b/server_fn/src/request/reqwest.rs @@ -56,14 +56,14 @@ impl ClientReq for Request { path: &str, accepts: &str, content_type: &str, - body: &[u8], + body: Bytes, ) -> Result> { let url = format!("{}{}", get_server_url(), path); CLIENT .post(url) .header(CONTENT_TYPE, content_type) .header(ACCEPT, accepts) - .body(body.to_owned()) + .body(body) .build() .map_err(|e| ServerFnError::Request(e.to_string())) }