HeaderMap

Struct HeaderMap 

Source
pub struct HeaderMap { /* private fields */ }
Expand description

Wrapper for HTTP headers used in request construction.

Implementations§

Source§

impl HeaderMap

Source

pub fn new() -> Self

Create a new empty HeaderMap.

Source

pub fn for_json() -> Result<Self>

Create HeaderMap with default headers for JSON requests.

Sets:

  • Content-Type: application/json
  • Accept: 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?;
Source

pub fn for_form() -> Self

Create HeaderMap for form-data requests.

Returns an empty HeaderMap because:

  • Content-Type is automatically set by post_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?;
Source

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())?;
Source

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);
Source

pub fn inner(&self) -> &HeaderMap

Get reference to the internal reqwest HeaderMap.

Source

pub fn inner_mut(&mut self) -> &mut HeaderMap

Get mutable reference to the internal reqwest HeaderMap.

Source

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);
Source

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"));
Source

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§

Source§

impl Clone for HeaderMap

Source§

fn clone(&self) -> HeaderMap

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HeaderMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HeaderMap

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more