pub struct HeaderMap { /* private fields */ }Expand description
Wrapper for HTTP headers used in request construction.
Implementations§
Source§impl HeaderMap
impl HeaderMap
Sourcepub fn for_json() -> Result<Self>
pub fn for_json() -> Result<Self>
Create HeaderMap with default headers for JSON requests.
Sets:
Content-Type: application/jsonAccept: application/json
§Example
use toolcraft_request::{Request, HeaderMap};
use serde_json::json;
let mut client = Request::new()?;
// Use preset JSON headers
let headers = HeaderMap::for_json()?;
client.set_default_headers(headers);
let body = json!({"key": "value"});
let response = client.post("/api", &body, None).await?;Sourcepub fn for_form() -> Self
pub fn for_form() -> Self
Create HeaderMap for form-data requests.
Returns an empty HeaderMap because:
Content-Typeis automatically set bypost_form()with the correct boundary- Manual setting would break multipart/form-data encoding
§Example
use toolcraft_request::{FormField, HeaderMap, Request};
let mut client = Request::new()?;
// For form uploads, use empty headers or add custom ones
let headers = HeaderMap::for_form();
client.set_default_headers(headers);
let fields = vec![FormField::text("name", "value")];
let response = client.post_form("/upload", fields, None).await?;Sourcepub fn insert(&mut self, key: impl AsRef<str>, value: String) -> Result<()>
pub fn insert(&mut self, key: impl AsRef<str>, value: String) -> Result<()>
Insert a header key-value pair. If the key already exists, the old value is replaced.
§Example
use toolcraft_request::HeaderMap;
let mut headers = HeaderMap::new();
headers.insert("Authorization", "Bearer token".to_string())?;
// Dynamic header names are supported
let header_name = "X-Custom-Header".to_string();
headers.insert(header_name, "value".to_string())?;Sourcepub fn get(&self, key: impl AsRef<str>) -> Option<String>
pub fn get(&self, key: impl AsRef<str>) -> Option<String>
Get the value of a header as String.
§Example
use toolcraft_request::HeaderMap;
let mut headers = HeaderMap::new();
headers.insert("Content-Type", "application/json".to_string())?;
assert_eq!(
headers.get("Content-Type"),
Some("application/json".to_string())
);
assert_eq!(headers.get("Missing"), None);Sourcepub fn inner_mut(&mut self) -> &mut HeaderMap
pub fn inner_mut(&mut self) -> &mut HeaderMap
Get mutable reference to the internal reqwest HeaderMap.
Sourcepub fn remove(&mut self, key: impl AsRef<str>) -> Option<String>
pub fn remove(&mut self, key: impl AsRef<str>) -> Option<String>
Remove a header by key and return its value if it existed.
§Example
use toolcraft_request::HeaderMap;
let mut headers = HeaderMap::new();
headers.insert("Content-Type", "application/json".to_string())?;
let removed = headers.remove("Content-Type");
assert_eq!(removed, Some("application/json".to_string()));
assert_eq!(headers.get("Content-Type"), None);Sourcepub fn contains(&self, key: impl AsRef<str>) -> bool
pub fn contains(&self, key: impl AsRef<str>) -> bool
Check if a header exists.
§Example
use toolcraft_request::HeaderMap;
let mut headers = HeaderMap::new();
headers.insert("Authorization", "Bearer token".to_string())?;
assert!(headers.contains("Authorization"));
assert!(!headers.contains("Missing"));Sourcepub fn merge(&mut self, other: HeaderMap)
pub fn merge(&mut self, other: HeaderMap)
Merge another HeaderMap into this one.
If a key exists in both, the value from other will overwrite.
§Example
use toolcraft_request::HeaderMap;
let mut default_headers = HeaderMap::new();
default_headers.insert("User-Agent", "MyApp/1.0".to_string())?;
let mut custom_headers = HeaderMap::new();
custom_headers.insert("Authorization", "Bearer token".to_string())?;
default_headers.merge(custom_headers);Trait Implementations§
Auto Trait Implementations§
impl Freeze for HeaderMap
impl RefUnwindSafe for HeaderMap
impl Send for HeaderMap
impl Sync for HeaderMap
impl Unpin for HeaderMap
impl UnwindSafe for HeaderMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more