seanmonstar / reqwest

An easy and powerful Rust HTTP Client
https://docs.rs/reqwest
Apache License 2.0
9.92k stars 1.12k forks source link

Multiple body builder methods in the same request silently overwrite the body #1615

Open BartMassey opened 2 years ago

BartMassey commented 2 years ago

In this example

let client = reqwest::blocking::Client::new();
client.post(url).body("hello").body("world").send().unwrap();

the client will post with a body of just "world".

I think it would be better if the second body() builder returned an Err at runtime. (It would be better to track this all with typestate to get compile-time errors, but that would be an impossible API change at this point.)

Issue inspired by this Reddit post. Putting both a form() and body() in the same request seems like a reasonably easy mistake to make, and could result in wasted work at best and really confusing behavior at worst.

Xuanwo commented 2 years ago

I think it would be better if the second body() builder returned an Err at runtime.

Returning an Err is impossible based on the current API:

pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder {}
BartMassey commented 2 years ago

Ugh. Good point; don't know what I was thinking. panic(), I guess?