reqwest
reqwest copied to clipboard
Allow overriding the default_headers of a client, rather than adding to it
Calling default_headers on a client will add to its internal HeaderMap, rather than replace it.
This is an issue for me, as something like the following:
use reqwest::header::{HeaderMap, ACCEPT, USER_AGENT};
fn main() {
let mut map = HeaderMap::new();
map.insert("some_header", "some_value".parse().unwrap());
map.insert(USER_AGENT, "my_agent".parse().unwrap());
map.insert(ACCEPT, "text/html".parse().unwrap());
let client = reqwest::blocking::Client::builder()
.default_headers(map)
.build()
.unwrap();
let response = client.get("https://tls.peet.ws/api/all").send().unwrap();
println!("{}", response.text().unwrap());
}
Leaves ACCEPT at the top:
...
"headers": [
"accept: text/html",
"some_header: some_value",
"user-agent: my_agent",
"host: tls.peet.ws"
]
...
Header ordering is not supposed to matter, but given that the function takes the new map by value, could its signature be changed to the following?
pub fn default_headers(mut self, headers: HeaderMap) -> ClientBuilder {
- for (key, value) in headers.iter() {
- self.config.headers.insert(key, value.clone());
- }
+ self.config.headers = headers;
self
}
A separate function would also work.