expression

Split and reconstruct a request

Consume a request to obtain its metadata and body separately. This pattern is useful when transforming a body without discarding headers, method or URI. External crate example; constructs data in memory, does not send requests. Expected stdout (not executed): POST 4 Common mistake: Attempting to access the original request after into_parts consumes it.

Formal Syntax & Structure

rust syntaxrust
request.into_parts(); Request::from_parts(parts, body)

Working Example

Example Snippetrust
fn main() {    use http::Request;    let req = Request::builder().method("POST").uri("/notes").body(String::from("note")).unwrap();    let (parts, body) = req.into_parts();    let transformed = Request::from_parts(parts, body.into_bytes());    println!("{} {}", transformed.method(), transformed.body().len());}
Rust Reference