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.rs (line 8)
3
4
5
6
7
8
9
10
fn main() {
    //Sends a HTTP HEAD request and processes the response.
    let res = request::head("https://www.rust-lang.org/learn").unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
}
More examples
Hide additional examples
examples/get.rs (line 11)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    //Container for body of a response.
    let mut body = Vec::new();

    //Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
    let res = request::get("https://www.rust-lang.org/learn", &mut body).unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
    //println!("{}", String::from_utf8_lossy(&body));
}
examples/post.rs (line 14)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    //Container for body of a response.
    let mut res_body = Vec::new();

    //Body of a request.
    const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2";

    //Sends a HTTP POST request and processes the response.
    let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
    println!("{}", String::from_utf8_lossy(&res_body));
}
examples/request_builder_get.rs (line 25)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    //Parses a URI and assigns it to a variable `addr`.
    let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();

    //Connects to a remote host. Uses information from `addr`.
    let stream = TcpStream::connect((addr.host().unwrap(), addr.corr_port())).unwrap();

    //Opens a secure connection over TlsStream. This is required due to use of `https` protocol.
    let mut stream = tls::Config::default()
        .connect(addr.host().unwrap_or(""), stream)
        .unwrap();

    //Container for a response's body.
    let mut writer = Vec::new();

    //Adds a header `Connection: Close`.
    let response = RequestBuilder::new(&addr)
        .header("Connection", "Close")
        .send(&mut stream, &mut writer)
        .unwrap();

    println!("Status: {} {}", response.status_code(), response.reason());
    println!("Headers: {}", response.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.rs (line 8)
3
4
5
6
7
8
9
10
fn main() {
    //Sends a HTTP HEAD request and processes the response.
    let res = request::head("https://www.rust-lang.org/learn").unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
}
More examples
Hide additional examples
examples/get.rs (line 11)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    //Container for body of a response.
    let mut body = Vec::new();

    //Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
    let res = request::get("https://www.rust-lang.org/learn", &mut body).unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
    //println!("{}", String::from_utf8_lossy(&body));
}
examples/post.rs (line 14)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    //Container for body of a response.
    let mut res_body = Vec::new();

    //Body of a request.
    const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2";

    //Sends a HTTP POST request and processes the response.
    let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
    println!("{}", String::from_utf8_lossy(&res_body));
}
examples/request_builder_get.rs (line 25)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    //Parses a URI and assigns it to a variable `addr`.
    let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();

    //Connects to a remote host. Uses information from `addr`.
    let stream = TcpStream::connect((addr.host().unwrap(), addr.corr_port())).unwrap();

    //Opens a secure connection over TlsStream. This is required due to use of `https` protocol.
    let mut stream = tls::Config::default()
        .connect(addr.host().unwrap_or(""), stream)
        .unwrap();

    //Container for a response's body.
    let mut writer = Vec::new();

    //Adds a header `Connection: Close`.
    let response = RequestBuilder::new(&addr)
        .header("Connection", "Close")
        .send(&mut stream, &mut writer)
        .unwrap();

    println!("Status: {} {}", response.status_code(), response.reason());
    println!("Headers: {}", response.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.rs (line 9)
3
4
5
6
7
8
9
10
fn main() {
    //Sends a HTTP HEAD request and processes the response.
    let res = request::head("https://www.rust-lang.org/learn").unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
}
More examples
Hide additional examples
examples/get.rs (line 12)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    //Container for body of a response.
    let mut body = Vec::new();

    //Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
    let res = request::get("https://www.rust-lang.org/learn", &mut body).unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
    //println!("{}", String::from_utf8_lossy(&body));
}
examples/post.rs (line 15)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    //Container for body of a response.
    let mut res_body = Vec::new();

    //Body of a request.
    const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2";

    //Sends a HTTP POST request and processes the response.
    let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap();

    //Prints details about the response.
    println!("Status: {} {}", res.status_code(), res.reason());
    println!("Headers: {}", res.headers());
    println!("{}", String::from_utf8_lossy(&res_body));
}
examples/request_builder_get.rs (line 26)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    //Parses a URI and assigns it to a variable `addr`.
    let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();

    //Connects to a remote host. Uses information from `addr`.
    let stream = TcpStream::connect((addr.host().unwrap(), addr.corr_port())).unwrap();

    //Opens a secure connection over TlsStream. This is required due to use of `https` protocol.
    let mut stream = tls::Config::default()
        .connect(addr.host().unwrap_or(""), stream)
        .unwrap();

    //Container for a response's body.
    let mut writer = Vec::new();

    //Adds a header `Connection: Close`.
    let response = RequestBuilder::new(&addr)
        .header("Connection", "Close")
        .send(&mut stream, &mut writer)
        .unwrap();

    println!("Status: {} {}", response.status_code(), response.reason());
    println!("Headers: {}", response.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 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 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> 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,

§

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 T
where 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 T
where 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.