Headers

Struct Headers 

Source
pub struct Headers(/* private fields */);
Expand description

Wrapper around HashMap<Ascii, String> with additional functionality for parsing HTTP headers

§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

Source

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

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

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, &String).

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

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

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

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

Trait Implementations§

Source§

impl Clone for Headers

Source§

fn clone(&self) -> Headers

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 Headers

Source§

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

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

impl Default for Headers

Source§

fn default() -> Headers

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

impl Display for Headers

Source§

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

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

impl From<HashMap<Ascii<String>, String>> for Headers

Source§

fn from(map: HashMap<Ascii<String>, String>) -> Headers

Converts to this type from the input type.
Source§

impl From<Headers> for HashMap<Ascii<String>, String>

Source§

fn from(map: Headers) -> HashMap<Ascii<String>, String>

Converts to this type from the input type.
Source§

impl FromStr for Headers

Source§

type Err = ParseErr

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Headers, ParseErr>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Headers

Source§

fn eq(&self, other: &Headers) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Headers

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, 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.