http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.45k stars 119 forks source link

Add specialized body types to `RequestBuilder` #302

Closed yoshuawuyts closed 3 years ago

yoshuawuyts commented 3 years ago

I was doing a coding exercise and realized that both surf::RequestBuilder and tide::ResponseBuilder didn't have body shorthand methods yet. This makes it so surf::RequestBuilder is closer to surf::Request, making it easier to write. Thanks!

Example

let uri = "https://httpbin.org/post";
let data = &Ip { ip: "129.0.0.1".into() };

// before
let res = surf::post(uri).body(Body::from_json(data)?).await?;

// after
let res = surf::post(uri).body_json(data)?.await?;