Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub version: HttpVersion,
    pub status_code: u16,
    pub reason_phrase: String,
    pub headers: Vec<Header>,
    pub trailers: Vec<Header>,
    pub body: Vec<u8>,
}
Expand description

Parsed HTTP response data.

Fields§

§version: HttpVersion

HTTP version parsed from the status line.

§status_code: u16

Numeric status code, for example 200 or 404.

§reason_phrase: String

Reason phrase from the status line, for example OK.

§headers: Vec<Header>

Response headers in wire order. Duplicate header names are preserved.

§trailers: Vec<Header>

Chunked transfer trailers, when present.

§body: Vec<u8>

Raw response body bytes.

Implementations§

Source§

impl Response

Source

pub fn header(&self, name: &str) -> Option<&str>

Returns the first header value matching name, using ASCII case-insensitive lookup.

Examples found in repository?
examples/head-request/main.rs (line 12)
3fn main() -> Result<(), Box<dyn Error>> {
4    let url = "http://example.com";
5    let response = nano_get::head(url)?;
6
7    println!("HEAD {url}");
8    println!(
9        "status: {} {}",
10        response.status_code, response.reason_phrase
11    );
12    println!("content-type: {:?}", response.header("content-type"));
13    println!("content-length: {:?}", response.header("content-length"));
14
15    let server_headers = response
16        .headers_named("server")
17        .map(|header| header.value().to_string())
18        .collect::<Vec<_>>();
19    println!("server headers: {server_headers:?}");
20    println!("body length after HEAD: {}", response.body.len());
21
22    Ok(())
23}
More examples
Hide additional examples
examples/request-builder/main.rs (line 33)
14fn main() -> Result<(), Box<dyn Error>> {
15    let url = "http://example.com";
16    let mut request = Request::get(url)?.with_redirect_policy(RedirectPolicy::follow(5));
17
18    request.add_header("Accept", "text/html,application/xhtml+xml")?;
19    request.add_header("Cache-Control", "max-age=0")?;
20    request.if_none_match("\"demo-etag\"")?;
21    request.if_modified_since(UNIX_EPOCH + Duration::from_secs(784_111_777))?;
22    request.range_bytes(Some(0), Some(255))?;
23
24    // Protocol-managed headers such as Host and Connection are intentionally rejected by the
25    // library. This example only uses safe end-to-end request headers.
26    let response = request.execute()?;
27
28    println!("manual request to {url}");
29    println!(
30        "status: {} {}",
31        response.status_code, response.reason_phrase
32    );
33    println!("content-type: {:?}", response.header("content-type"));
34    println!("body preview: {}", preview_text(response.body_text()?, 80));
35
36    Ok(())
37}
Source

pub fn headers_named<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = &'a Header> + 'a

Iterates over all header values matching name, preserving wire order and duplicates.

Examples found in repository?
examples/head-request/main.rs (line 16)
3fn main() -> Result<(), Box<dyn Error>> {
4    let url = "http://example.com";
5    let response = nano_get::head(url)?;
6
7    println!("HEAD {url}");
8    println!(
9        "status: {} {}",
10        response.status_code, response.reason_phrase
11    );
12    println!("content-type: {:?}", response.header("content-type"));
13    println!("content-length: {:?}", response.header("content-length"));
14
15    let server_headers = response
16        .headers_named("server")
17        .map(|header| header.value().to_string())
18        .collect::<Vec<_>>();
19    println!("server headers: {server_headers:?}");
20    println!("body length after HEAD: {}", response.body.len());
21
22    Ok(())
23}
Source

pub fn trailer(&self, name: &str) -> Option<&str>

Returns the first trailer value matching name, using ASCII case-insensitive lookup.

Source

pub fn www_authenticate_challenges( &self, ) -> Result<Vec<Challenge>, NanoGetError>

Parses WWW-Authenticate challenges from the response.

Examples found in repository?
examples/custom-auth-handler/main.rs (line 59)
41fn main() -> Result<(), Box<dyn Error>> {
42    let Some(url) = env::var("NANO_GET_CUSTOM_AUTH_URL").ok() else {
43        print_setup();
44        return Ok(());
45    };
46
47    let client = Client::builder()
48        .auth_handler(Arc::new(DemoTokenAuth))
49        .build();
50    let response = client.execute(Request::get(&url)?)?;
51
52    println!("custom-auth-handler example");
53    println!(
54        "status: {} {}",
55        response.status_code, response.reason_phrase
56    );
57    println!(
58        "parsed WWW-Authenticate challenges: {}",
59        response.www_authenticate_challenges()?.len()
60    );
61
62    Ok(())
63}
Source

pub fn proxy_authenticate_challenges( &self, ) -> Result<Vec<Challenge>, NanoGetError>

Parses Proxy-Authenticate challenges from the response.

Source

pub fn body_text(&self) -> Result<&str, NanoGetError>

Decodes the body as UTF-8 without taking ownership.

Examples found in repository?
examples/request-builder/main.rs (line 34)
14fn main() -> Result<(), Box<dyn Error>> {
15    let url = "http://example.com";
16    let mut request = Request::get(url)?.with_redirect_policy(RedirectPolicy::follow(5));
17
18    request.add_header("Accept", "text/html,application/xhtml+xml")?;
19    request.add_header("Cache-Control", "max-age=0")?;
20    request.if_none_match("\"demo-etag\"")?;
21    request.if_modified_since(UNIX_EPOCH + Duration::from_secs(784_111_777))?;
22    request.range_bytes(Some(0), Some(255))?;
23
24    // Protocol-managed headers such as Host and Connection are intentionally rejected by the
25    // library. This example only uses safe end-to-end request headers.
26    let response = request.execute()?;
27
28    println!("manual request to {url}");
29    println!(
30        "status: {} {}",
31        response.status_code, response.reason_phrase
32    );
33    println!("content-type: {:?}", response.header("content-type"));
34    println!("body preview: {}", preview_text(response.body_text()?, 80));
35
36    Ok(())
37}
Source

pub fn into_body_text(self) -> Result<String, NanoGetError>

Consumes the response and decodes the body as UTF-8.

Source

pub fn is_success(&self) -> bool

Returns true when status is in the 2xx range.

Source

pub fn is_redirection(&self) -> bool

Returns true when status is in the 3xx range.

Source

pub fn is_client_error(&self) -> bool

Returns true when status is in the 4xx range.

Source

pub fn is_server_error(&self) -> bool

Returns true when status is in the 5xx range.

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

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

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 Eq for Response

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