pub struct Headers(_);
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();
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let mut writer = Vec::new();
let uri = Uri::try_from("http://eu.httpbin.org/get?msg=WasmEdge").unwrap();
// let uri = Uri::try_from("https://httpbin.org/get").unwrap(); // uncomment the line for https request
// add headers to the request
let mut headers = Headers::new();
headers.insert("Accept-Charset", "utf-8");
headers.insert("Accept-Language", "en-US");
headers.insert("Host", "rust-lang.org");
headers.insert("Connection", "Close");
Request::new(&uri)
.headers(headers)
.send(&mut writer)
.unwrap();
println!("{}", String::from_utf8_lossy(&writer));
// set version
Request::new(&uri)
.version(HttpVersion::Http10)
.send(&mut writer)
.unwrap();
println!("{}", String::from_utf8_lossy(&writer));
}
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>where
T: ToString + ?Sized,
U: ToString + ?Sized,
pub fn insert<T, U>(&mut self, key: &T, val: &U) -> Option<String>where T: ToString + ?Sized, U: ToString + ?Sized,
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");
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let mut writer = Vec::new();
let uri = Uri::try_from("http://eu.httpbin.org/get?msg=WasmEdge").unwrap();
// let uri = Uri::try_from("https://httpbin.org/get").unwrap(); // uncomment the line for https request
// add headers to the request
let mut headers = Headers::new();
headers.insert("Accept-Charset", "utf-8");
headers.insert("Accept-Language", "en-US");
headers.insert("Host", "rust-lang.org");
headers.insert("Connection", "Close");
Request::new(&uri)
.headers(headers)
.send(&mut writer)
.unwrap();
println!("{}", String::from_utf8_lossy(&writer));
// set version
Request::new(&uri)
.version(HttpVersion::Http10)
.send(&mut writer)
.unwrap();
println!("{}", String::from_utf8_lossy(&writer));
}
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);