Struct http_req::response::Response

source ·
pub struct Response { /* private fields */ }
Expand description

Represents an HTTP response.

It contains Headers and Status parsed from response.

Implementations§

source§

impl Response

source

pub fn from_head(head: &[u8]) -> Result<Response, Error>

Creates new Response with head - status and headers - parsed from a slice of bytes

Examples
use http_req::response::Response;

const HEAD: &[u8; 102] = b"HTTP/1.1 200 OK\r\n\
                         Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                         Content-Type: text/html\r\n\
                         Content-Length: 100\r\n\r\n";

let response = Response::from_head(HEAD).unwrap();
source

pub fn try_from<T: Write>(res: &[u8], writer: &mut T) -> Result<Response, Error>

Parses Response from slice of bytes. Writes it’s body to writer.

Examples
use http_req::response::Response;

const RESPONSE: &[u8; 129] = b"HTTP/1.1 200 OK\r\n\
                             Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                             Content-Type: text/html\r\n\
                             Content-Length: 100\r\n\r\n\
                             <html>hello\r\n\r\nhello</html>";
let mut body = Vec::new();

let response = Response::try_from(RESPONSE, &mut body).unwrap();
source

pub const fn status_code(&self) -> StatusCode

Returns status code of this Response.

Examples
use http_req::response::{Response, StatusCode};

const RESPONSE: &[u8; 129] = b"HTTP/1.1 200 OK\r\n\
                             Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                             Content-Type: text/html\r\n\
                             Content-Length: 100\r\n\r\n\
                             <html>hello\r\n\r\nhello</html>";
let mut body = Vec::new();

let response = Response::try_from(RESPONSE, &mut body).unwrap();
assert_eq!(response.status_code(), StatusCode::new(200));
Examples found in repository?
examples/head_https.rs (line 6)
3
4
5
6
7
8
fn main() {
    let res = request::head("https://httpbin.org/headers").unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("{:?}", res.headers());
}
More examples
Hide additional examples
examples/head.rs (line 6)
3
4
5
6
7
8
fn main() {
    let res = request::head("http://eu.httpbin.org/headers").unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("{:?}", res.headers());
}
examples/get_https.rs (line 7)
3
4
5
6
7
8
9
10
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    let res = request::get("https://httpbin.org/get?msg=WasmEdge", &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/get.rs (line 7)
3
4
5
6
7
8
9
10
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    let res = request::get("http://eu.httpbin.org/get?msg=WasmEdge", &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/post_https.rs (line 8)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    const BODY: &[u8; 27] = b"field1=value1&field2=value2";
    let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/post.rs (line 8)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    const BODY: &[u8; 27] = b"field1=value1&field2=value2";
    let res = request::post("http://eu.httpbin.org/post", BODY, &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
source

pub fn version(&self) -> &str

Returns HTTP version of this Response.

Examples
use http_req::response::Response;

const RESPONSE: &[u8; 129] = b"HTTP/1.1 200 OK\r\n\
                             Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                             Content-Type: text/html\r\n\
                             Content-Length: 100\r\n\r\n\
                             <html>hello\r\n\r\nhello</html>";
let mut body = Vec::new();

let response = Response::try_from(RESPONSE, &mut body).unwrap();
assert_eq!(response.version(), "HTTP/1.1");
source

pub fn reason(&self) -> &str

Returns reason of this Response.

Examples
use http_req::response::Response;

const RESPONSE: &[u8; 129] = b"HTTP/1.1 200 OK\r\n\
                             Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                             Content-Type: text/html\r\n\
                             Content-Length: 100\r\n\r\n\
                             <html>hello\r\n\r\nhello</html>";
let mut body = Vec::new();

let response = Response::try_from(RESPONSE, &mut body).unwrap();
assert_eq!(response.reason(), "OK");
Examples found in repository?
examples/head_https.rs (line 6)
3
4
5
6
7
8
fn main() {
    let res = request::head("https://httpbin.org/headers").unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("{:?}", res.headers());
}
More examples
Hide additional examples
examples/head.rs (line 6)
3
4
5
6
7
8
fn main() {
    let res = request::head("http://eu.httpbin.org/headers").unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("{:?}", res.headers());
}
examples/get_https.rs (line 7)
3
4
5
6
7
8
9
10
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    let res = request::get("https://httpbin.org/get?msg=WasmEdge", &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/get.rs (line 7)
3
4
5
6
7
8
9
10
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    let res = request::get("http://eu.httpbin.org/get?msg=WasmEdge", &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/post_https.rs (line 8)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    const BODY: &[u8; 27] = b"field1=value1&field2=value2";
    let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/post.rs (line 8)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    const BODY: &[u8; 27] = b"field1=value1&field2=value2";
    let res = request::post("http://eu.httpbin.org/post", BODY, &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
source

pub fn headers(&self) -> &Headers

Returns headers of this Response.

Examples
use http_req::response::Response;

const RESPONSE: &[u8; 129] = b"HTTP/1.1 200 OK\r\n\
                             Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                             Content-Type: text/html\r\n\
                             Content-Length: 100\r\n\r\n\
                             <html>hello\r\n\r\nhello</html>";
let mut body = Vec::new();

let response = Response::try_from(RESPONSE, &mut body).unwrap();
let headers = response.headers();
Examples found in repository?
examples/head_https.rs (line 7)
3
4
5
6
7
8
fn main() {
    let res = request::head("https://httpbin.org/headers").unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("{:?}", res.headers());
}
More examples
Hide additional examples
examples/head.rs (line 7)
3
4
5
6
7
8
fn main() {
    let res = request::head("http://eu.httpbin.org/headers").unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("{:?}", res.headers());
}
examples/get_https.rs (line 8)
3
4
5
6
7
8
9
10
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    let res = request::get("https://httpbin.org/get?msg=WasmEdge", &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/get.rs (line 8)
3
4
5
6
7
8
9
10
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    let res = request::get("http://eu.httpbin.org/get?msg=WasmEdge", &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/post_https.rs (line 9)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    const BODY: &[u8; 27] = b"field1=value1&field2=value2";
    let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
examples/post.rs (line 9)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut writer = Vec::new(); //container for body of a response
    const BODY: &[u8; 27] = b"field1=value1&field2=value2";
    let res = request::post("http://eu.httpbin.org/post", BODY, &mut writer).unwrap();

    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers {}", res.headers());
    println!("{}", String::from_utf8_lossy(&writer));
}
source

pub fn content_len(&self) -> Option<usize>

Returns length of the content of this Response as a Option, according to information included in headers. If there is no such an information, returns None.

Examples
use http_req::response::Response;

const RESPONSE: &[u8; 129] = b"HTTP/1.1 200 OK\r\n\
                             Date: Sat, 11 Jan 2003 02:44:04 GMT\r\n\
                             Content-Type: text/html\r\n\
                             Content-Length: 100\r\n\r\n\
                             <html>hello\r\n\r\nhello</html>";
let mut body = Vec::new();

let response = Response::try_from(RESPONSE, &mut body).unwrap();
assert_eq!(response.content_len().unwrap(), 100);

Trait Implementations§

source§

impl Clone for Response

source§

fn clone(&self) -> Response

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 Response

source§

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

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

impl PartialEq<Response> for Response

source§

fn eq(&self, other: &Response) -> 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 Response

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, 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.