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: HttpVersionHTTP version parsed from the status line.
status_code: u16Numeric status code, for example 200 or 404.
reason_phrase: StringReason 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
impl Response
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Returns the first header value matching name, using ASCII case-insensitive lookup.
Examples found in repository?
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
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}Sourcepub fn headers_named<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = &'a Header> + 'a
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?
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}Sourcepub fn trailer(&self, name: &str) -> Option<&str>
pub fn trailer(&self, name: &str) -> Option<&str>
Returns the first trailer value matching name, using ASCII case-insensitive lookup.
Sourcepub fn www_authenticate_challenges(
&self,
) -> Result<Vec<Challenge>, NanoGetError>
pub fn www_authenticate_challenges( &self, ) -> Result<Vec<Challenge>, NanoGetError>
Parses WWW-Authenticate challenges from the response.
Examples found in repository?
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}Sourcepub fn proxy_authenticate_challenges(
&self,
) -> Result<Vec<Challenge>, NanoGetError>
pub fn proxy_authenticate_challenges( &self, ) -> Result<Vec<Challenge>, NanoGetError>
Parses Proxy-Authenticate challenges from the response.
Sourcepub fn body_text(&self) -> Result<&str, NanoGetError>
pub fn body_text(&self) -> Result<&str, NanoGetError>
Decodes the body as UTF-8 without taking ownership.
Examples found in repository?
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}Sourcepub fn into_body_text(self) -> Result<String, NanoGetError>
pub fn into_body_text(self) -> Result<String, NanoGetError>
Consumes the response and decodes the body as UTF-8.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Returns true when status is in the 2xx range.
Sourcepub fn is_redirection(&self) -> bool
pub fn is_redirection(&self) -> bool
Returns true when status is in the 3xx range.
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Returns true when status is in the 4xx range.
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Returns true when status is in the 5xx range.