reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

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

Open BartMassey opened this issue 2 years ago • 2 comments

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.

BartMassey avatar Sep 03 '22 16:09 BartMassey

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 {}

Xuanwo avatar Sep 06 '22 13:09 Xuanwo

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

BartMassey avatar Sep 06 '22 15:09 BartMassey