pub struct Headers(/* private fields */);
Expand description
Wrapper around HashMap<Ascii
§Example
use http_req::response::Headers;
let mut headers = Headers::new();
headers.insert("Connection", "Close");
assert_eq!(headers.get("Connection"), Some(&"Close".to_string()))
Implementations§
source§impl Headers
impl Headers
sourcepub fn new() -> Headers
pub fn new() -> Headers
Creates an empty Headers
.
The headers are initially created with a capacity of 0, so they will not allocate until it is first inserted into.
§Examples
use http_req::response::Headers;
let mut headers = Headers::new();
sourcepub fn with_capacity(capacity: usize) -> Headers
pub fn with_capacity(capacity: usize) -> Headers
Creates empty Headers
with the specified capacity.
The headers will be able to hold at least capacity elements without reallocating. If capacity is 0, the headers will not allocate.
§Examples
use http_req::response::Headers;
let mut headers = Headers::with_capacity(200);
sourcepub fn iter(&self) -> Iter<'_, Ascii<String>, String>
pub fn iter(&self) -> Iter<'_, Ascii<String>, String>
An iterator visiting all key-value pairs in arbitrary order.
The iterator’s element type is (&Ascii
§Examples
use http_req::response::Headers;
let mut headers = Headers::new();
headers.insert("Accept-Charset", "utf-8");
headers.insert("Accept-Language", "en-US");
headers.insert("Connection", "Close");
let mut iterator = headers.iter();
sourcepub fn get<T: ToString + ?Sized>(&self, k: &T) -> Option<&String>
pub fn get<T: ToString + ?Sized>(&self, k: &T) -> Option<&String>
Returns a reference to the value corresponding to the key.
§Examples
use http_req::response::Headers;
let mut headers = Headers::new();
headers.insert("Accept-Charset", "utf-8");
assert_eq!(headers.get("Accept-Charset"), Some(&"utf-8".to_string()))
sourcepub fn insert<T, U>(&mut self, key: &T, val: &U) -> Option<String>
pub fn insert<T, U>(&mut self, key: &T, val: &U) -> Option<String>
Inserts a key-value pair into the headers.
If the headers did not have this key present, None is returned.
If the headers did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical.
§Examples
use http_req::response::Headers;
let mut headers = Headers::new();
headers.insert("Accept-Language", "en-US");
sourcepub fn default_http(uri: &Uri<'_>) -> Headers
pub fn default_http(uri: &Uri<'_>) -> Headers
Creates default headers for a HTTP request
§Examples
use http_req::{response::Headers, uri::Uri};
use std::convert::TryFrom;
let uri: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
let headers = Headers::default_http(&uri);