Skip to content

Commit 5bf947e

Browse files
committed
feat(fetch): Header and Headers upd
1 parent 4b3abed commit 5bf947e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

examples/fetch/src/post.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
3232
let request = Request::new("/")
3333
.method(Method::Post)
3434
.header(Header::custom("Accept-Language", "en"))
35-
.header(Header::custom("Authorization", format!("Basic {}", token)))
35+
.header(Header::bearer(token))
3636
.json(&model.form)
3737
.expect("Serialization failed");
3838

src/browser/fetch/header.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ impl<'a> Headers<'a> {
1010
/// Sets a new value for an existing header or adds the header if
1111
/// it does not already exist.
1212
pub fn set(&mut self, header: Header<'a>) {
13-
self.0.retain(|Header { name, .. }| &header.name != name);
13+
self.0.retain(|old_header| header.name != old_header.name);
14+
self.0.push(header);
15+
}
16+
17+
/// Add the header.
18+
///
19+
/// Headers with the same name are not modified or removed.
20+
pub fn add(&mut self, header: Header<'a>) {
1421
self.0.push(header);
1522
}
1623
}
@@ -33,7 +40,7 @@ pub struct Header<'a> {
3340
impl<'a> Header<'a> {
3441
/// Create `Content-Type` header.
3542
pub fn content_type(value: impl Into<Cow<'a, str>>) -> Header<'a> {
36-
Self::custom("Content-Type", value.into())
43+
Self::custom("Content-Type", value)
3744
}
3845

3946
/// Create `Authorization` header.
@@ -42,8 +49,8 @@ impl<'a> Header<'a> {
4249
}
4350

4451
/// Create `Authorization: Bearer xxx` header.
45-
pub fn bearer(value: impl Into<Cow<'a, str>>) -> Header<'a> {
46-
Self::custom("Authorization", format!("Bearer {}", value.into()))
52+
pub fn bearer(token: &str) -> Header<'a> {
53+
Self::custom("Authorization", format!("Bearer {}", token))
4754
}
4855

4956
/// Create custom header.

0 commit comments

Comments
 (0)