pub struct Response {
pub status_code: u16,
pub reason_phrase: String,
pub headers: Vec<(String, String)>,
pub url: String,
/* private fields */
}Expand description
An HTTP response.
Returned by Request::send.
§Example
let response = minreq::get("http://example.com").send()?;
println!("{}", response.as_str()?);Fields§
§status_code: u16The status code of the response, eg. 404.
reason_phrase: StringThe reason phrase of the response, eg. “Not Found”.
headers: Vec<(String, String)>The headers of the response, as (field name, value) tuples. Field
names are as they were sent by the server (not lowercased, as in minreq
v2).
url: StringThe URL of the resource returned in this response. May differ from the request URL if it was redirected or typo corrections were applied (e.g. http://example.com?foo=bar would be corrected to http://example.com/?foo=bar).
Implementations§
Source§impl Response
impl Response
Sourcepub fn as_str(&self) -> Result<&str, Error>
pub fn as_str(&self) -> Result<&str, Error>
Returns the body as an &str.
§Errors
Returns
InvalidUtf8InBody
if the body is not UTF-8, with a description as to why the
provided slice is not UTF-8.
§Example
let response = minreq::get(url).send()?;
println!("{}", response.as_str()?);Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns a reference to the contained bytes of the body. If you
want the Vec<u8> itself, use
into_bytes() instead.
§Example
let response = minreq::get(url).send()?;
println!("{:?}", response.as_bytes());Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Turns the Response into the inner Vec<u8>, the bytes that
make up the response’s body. If you just need a &[u8], use
as_bytes() instead.
§Example
let response = minreq::get(url).send()?;
println!("{:?}", response.into_bytes());
// This would error, as into_bytes consumes the Response:
// let x = response.status_code;Sourcepub fn header(&self, field_name: &str) -> Option<&str>
pub fn header(&self, field_name: &str) -> Option<&str>
Finds the first value of a header with the given field name, ignoring ASCII casing differences.
§Example
let response = minreq::get(url).send()?;
println!("Content size: {:?}", response.header("Content-Size"));Sourcepub fn headers<'a>(
&'a self,
field_name: &'a str,
) -> impl Iterator<Item = &'a str>
pub fn headers<'a>( &'a self, field_name: &'a str, ) -> impl Iterator<Item = &'a str>
Finds the values of headers with the given field name, ignoring ASCII casing differences.
§Example
let response = minreq::get(url).send()?;
let server_headers: Vec<&str> = response.headers("Server").collect::<Vec<_>>();
println!("Server(s) involved in this request: {:?}", server_headers);Sourcepub fn json<'a, T>(&'a self) -> Result<T, Error>where
T: Deserialize<'a>,
pub fn json<'a, T>(&'a self) -> Result<T, Error>where
T: Deserialize<'a>,
Converts JSON body to a struct using Serde.
§Errors
Returns
SerdeJsonError if
Serde runs into a problem, or
InvalidUtf8InBody
if the body is not UTF-8.
§Example
In case compiler cannot figure out return type you might need to declare it explicitly:
use serde_json::Value;
// Value could be any type that implements Deserialize!
let user = minreq::get(url_to_json_resource).send()?.json::<Value>()?;
println!("User name is '{}'", user["name"]);