Skip to content

Feature: Implement the Body trait for more native data types to improve ergonomics #3746

Open
@akneni

Description

@akneni

Currently, the 'String' is the only native data type that implements hyper::body::Body.
If we're passing other data types as a body, we need to add the http_body_util crate and add a little bit of boiler plate when building our request.

use http_body_util::Full;

let some_bytes = vec![0_u8, 1, 2];
let mut req = Request::builder()
    .method("POST")
    .body(Full::<Bytes>::from(some_bytes))
    .unwrap();

Note: While the request builder itself may not require the body to implement the Body trait, both the sender and connection returned by http1::handshake require the request's body to implement it.

With the body trait implemented for Vec<u8> (just one example that probably should implement body) we could leave out the http_body_util crate. Building a request would also become more intuitive and ergonomic.

    let some_bytes = vec![0_u8, 1, 2];
    let mut req = Request::builder()
        .method("POST")
        .body(some_bytes)
        .unwrap();

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-rfcBlocked: More comments would be useful in determine next steps.C-featureCategory: feature. This is adding a new feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions