Skip to main content

Response

Struct Response 

Source
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: u16

The status code of the response, eg. 404.

§reason_phrase: String

The 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: String

The 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

Source

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()?);
Examples found in repository?
examples/hello.rs (line 5)
3fn main() -> Result<(), minreq::Error> {
4    let response = minreq::get("http://example.com").send()?;
5    println!("{}", response.as_str()?);
6    Ok(())
7}
Source

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());
Source

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

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"));
Source

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);
Source

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"]);
Examples found in repository?
examples/json.rs (line 9)
3fn main() -> Result<(), minreq::Error> {
4    let response = minreq::get("http://httpbin.org/anything")
5        .with_body("Hello, world!")
6        .send()?;
7
8    // httpbin.org/anything returns the body in the json field "data":
9    let json: serde_json::Value = response.json()?;
10    println!("\"Hello, world!\" == {}", json["data"]);
11
12    Ok(())
13}

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Eq for Response

Source§

impl PartialEq for Response

Source§

fn eq(&self, other: &Response) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.