[−][src]Struct ureq::Response
Response instances are created as results of firing off requests.
The Response
is used to read response headers and decide what to do with the body.
Note that the socket connection is open and the body not read until one of
into_reader()
, into_json()
, or
into_string()
consumes the response.
let response = ureq::get("http://example.com/").call()?; // socket is still open and the response body has not been read. let text = response.into_string()?; // response is consumed, and body has been read.
Implementations
impl Response
[src]
pub fn new(
status: u16,
status_text: &str,
body: &str
) -> Result<Response, Error>
[src]
status: u16,
status_text: &str,
body: &str
) -> Result<Response, Error>
Construct a response with a status, status text and a string body.
This is hopefully useful for unit tests.
Example:
let resp = ureq::Response::new(401, "Authorization Required", "Please log in")?; assert_eq!(resp.status(), 401);
pub fn get_url(&self) -> &str
[src]
The URL we ended up at. This can differ from the request url when we have followed redirects.
pub fn http_version(&self) -> &str
[src]
The http version: HTTP/1.1
pub fn status(&self) -> u16
[src]
The status as a u16: 200
pub fn status_text(&self) -> &str
[src]
The status text: OK
pub fn header(&self, name: &str) -> Option<&str>
[src]
The header corresponding header value for the give name, if any.
pub fn headers_names(&self) -> Vec<String>
[src]
A list of the header names in this response. Lowercased to be uniform.
pub fn has(&self, name: &str) -> bool
[src]
Tells if the response has the named header.
pub fn all(&self, name: &str) -> Vec<&str>
[src]
All headers corresponding values for the give name, or empty vector.
pub fn content_type(&self) -> &str
[src]
The content type part of the "Content-Type" header without the charset.
Example:
let resp = ureq::get("http://example.com/").call()?; assert!(matches!(resp.header("content-type"), Some("text/html; charset=ISO-8859-1"))); assert_eq!("text/html", resp.content_type());
pub fn charset(&self) -> &str
[src]
The character set part of the "Content-Type".
Example:
let resp = ureq::get("http://example.com/").call()?; assert!(matches!(resp.header("content-type"), Some("text/html; charset=ISO-8859-1"))); assert_eq!("ISO-8859-1", resp.charset());
pub fn into_reader(self) -> impl Read + Send
[src]
Turn this response into a impl Read
of the body.
- If
Transfer-Encoding: chunked
, the returned reader will unchunk it and anyContent-Length
header is ignored. - If
Content-Length
is set, the returned reader is limited to this byte length regardless of how many bytes the server sends. - If no length header, the reader is until server stream end.
Example:
use std::io::Read; let resp = ureq::get("http://httpbin.org/bytes/100") .call()?; assert!(resp.has("Content-Length")); let len = resp.header("Content-Length") .and_then(|s| s.parse::<usize>().ok()).unwrap(); let mut bytes: Vec<u8> = Vec::with_capacity(len); resp.into_reader() .read_to_end(&mut bytes)?; assert_eq!(bytes.len(), len);
pub fn into_string(self) -> Result<String>
[src]
Turn this response into a String of the response body. By default uses utf-8
,
but can work with charset, see below.
This is potentially memory inefficient for large bodies since the
implementation first reads the reader to end into a Vec<u8>
and then
attempts to decode it using the charset.
Example:
let text = ureq::get("http://httpbin.org/get/success") .call()? .into_string()?; assert!(text.contains("success"));
Charset support
If you enable feature ureq = { version = "*", features = ["charset"] }
, into_string()
attempts to respect the character encoding of the Content-Type
header. If there is no
Content-Type header, or the Content-Type header does not specify a charset, into_string()
uses utf-8
.
I.e. Content-Length: text/plain; charset=iso-8859-1
would be decoded in latin-1.
pub fn into_json<T: DeserializeOwned>(self) -> Result<T>
[src]
Read the body of this response into a serde_json::Value, or any other type that
You must use either a type annotation as shown below (message: Message
), or the
turbofish operator (::<Type>
) so Rust knows what type you are trying to read.
Requires feature ureq = { version = "*", features = ["json"] }
Example:
use serde::{Deserialize, de::DeserializeOwned}; #[derive(Deserialize)] struct Message { hello: String, } let message: Message = ureq::get("http://example.com/hello_world.json") .call()? .into_json()?; assert_eq!(message.hello, "world");
Or, if you don't want to define a struct to read your JSON into, you can
use the convenient serde_json::Value
type to parse arbitrary or unknown
JSON.
let json: serde_json::Value = ureq::get("http://example.com/hello_world.json") .call()? .into_json()?; assert_eq!(json["hello"], "world");
Trait Implementations
impl Debug for Response
[src]
impl From<Response> for Error
[src]
impl FromStr for Response
[src]
type Err = Error
The associated error which can be returned from parsing.
pub fn from_str(s: &str) -> Result<Self, Self::Err>
[src]
Parse a response from a string.
Example:
let s = "HTTP/1.1 200 OK\r\n\ X-Forwarded-For: 1.2.3.4\r\n\ Content-Type: text/plain\r\n\ \r\n\ Hello World!!!"; let resp = s.parse::<ureq::Response>().unwrap(); assert!(resp.has("X-Forwarded-For")); let body = resp.into_string().unwrap(); assert_eq!(body, "Hello World!!!");
Auto Trait Implementations
impl !RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl !UnwindSafe for Response
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,