Struct http_req::response::Headers

source ·
pub struct Headers(_);
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)
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));
}
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)
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));
}
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 copy 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, RandomState>> 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

§

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<Headers> for Headers

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.