pub struct Response { /* private fields */ }
Expand description
Represents an HTTP response.
It contains Headers
and Status
parsed from response.
Implementations§
source§impl Response
impl Response
sourcepub fn from_head(head: &[u8]) -> Result<Response, Error>
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();
sourcepub fn try_from<T: Write>(res: &[u8], writer: &mut T) -> Result<Response, Error>
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();
sourcepub const fn status_code(&self) -> StatusCode
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?
More 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));
}
sourcepub fn version(&self) -> &str
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");
sourcepub fn reason(&self) -> &str
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?
More 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));
}
sourcepub fn headers(&self) -> &Headers
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?
More 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));
}
sourcepub fn content_len(&self) -> Option<usize>
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 PartialEq for Response
impl PartialEq for Response
impl StructuralPartialEq for Response
Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more