Struct ureq::Response [−][src]
pub struct Response { /* fields omitted */ }
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("https://www.google.com").call(); // socket is still open and the response body has not been read. let text = response.into_string().unwrap(); // response is consumed, and body has been read.
Methods
impl Response
[src]
impl Response
pub fn new(status: u16, status_text: &str, body: &str) -> Self
[src]
pub fn new(status: u16, status_text: &str, body: &str) -> Self
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 status_line(&self) -> &str
[src]
pub fn status_line(&self) -> &str
The entire status line like: HTTP/1.1 200 OK
pub fn http_version(&self) -> &str
[src]
pub fn http_version(&self) -> &str
The http version: HTTP/1.1
pub fn status(&self) -> u16
[src]
pub fn status(&self) -> u16
The status as a u16: 200
pub fn status_text(&self) -> &str
[src]
pub fn status_text(&self) -> &str
The status text: OK
pub fn header<'a>(&self, name: &'a str) -> Option<&str>
[src]
pub fn header<'a>(&self, name: &'a str) -> Option<&str>
The header corresponding header value for the give name, if any.
pub fn has<'a>(&self, name: &'a str) -> bool
[src]
pub fn has<'a>(&self, name: &'a str) -> bool
Tells if the response has the named header.
pub fn all<'a>(&self, name: &'a str) -> Vec<&str>
[src]
pub fn all<'a>(&self, name: &'a str) -> Vec<&str>
All headers corresponding values for the give name, or empty vector.
pub fn ok(&self) -> bool
[src]
pub fn ok(&self) -> bool
Whether the response status is: 200 <= status <= 299
pub fn redirect(&self) -> bool
[src]
pub fn redirect(&self) -> bool
pub fn client_error(&self) -> bool
[src]
pub fn client_error(&self) -> bool
Whether the response status is: 400 <= status <= 499
pub fn server_error(&self) -> bool
[src]
pub fn server_error(&self) -> bool
Whether the response status is: 500 <= status <= 599
pub fn error(&self) -> bool
[src]
pub fn error(&self) -> bool
Whether the response status is: 400 <= status <= 599
pub fn synthetic(&self) -> bool
[src]
pub fn synthetic(&self) -> bool
Tells if this response is "synthetic".
The methods firing
off
requests
all return a Response
; there is no rust style Result
.
Rather than exposing a custom error type through results, this library has opted for representing potential connection/TLS/etc errors as HTTP response codes. These invented codes are called "synthetic".
The idea is that from a library user's point of view the distinction of whether a failure originated in the remote server (500, 502) etc, or some transient network failure, the code path of handling that would most often be the same.
The specific mapping of error to code can be seen in the Error
doc.
However if the distinction is important, this method can be used to tell. Also see synthetic_error() to see the actual underlying error.
// scheme that this library doesn't understand let resp = ureq::get("borkedscheme://www.google.com").call(); // it's an error assert!(resp.error()); // synthetic error code 400 assert_eq!(resp.status(), 400); // tell that it's synthetic. assert!(resp.synthetic());
pub fn synthetic_error(&self) -> &Option<Error>
[src]
pub fn synthetic_error(&self) -> &Option<Error>
Get the actual underlying error when the response is "synthetic".
pub fn content_type(&self) -> &str
[src]
pub fn content_type(&self) -> &str
The content type part of the "Content-Type" header without the charset.
Example:
let resp = ureq::get("https://www.google.com/").call(); assert_eq!("text/html; charset=ISO-8859-1", resp.header("content-type").unwrap()); assert_eq!("text/html", resp.content_type());
pub fn charset(&self) -> &str
[src]
pub fn charset(&self) -> &str
The character set part of the "Content-Type" header.native_tls
Example:
let resp = ureq::get("https://www.google.com/").call(); assert_eq!("text/html; charset=ISO-8859-1", resp.header("content-type").unwrap()); assert_eq!("ISO-8859-1", resp.charset());
pub fn into_reader(
self
) -> impl Read
[src]
pub fn into_reader(
self
) -> impl Read
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("https://s3.amazonaws.com/foosrvr/hello_world.json") .call(); assert!(resp.has("Content-Length")); let len = resp.header("Content-Length") .and_then(|s| s.parse::<usize>().ok()).unwrap(); let mut reader = resp.into_reader(); let mut bytes = vec![]; reader.read_to_end(&mut bytes); assert_eq!(bytes.len(), len);
pub fn into_string(self) -> IoResult<String>
[src]
pub fn into_string(self) -> IoResult<String>
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 resp = ureq::get("https://s3.amazonaws.com/foosrvr/hello_world.json") .call(); let text = resp.into_string().unwrap(); assert!(text.contains("hello"));
Charset support
Requires feature ureq = { version = "*", features = ["charset"] }
Attempts to respect the character encoding of the Content-Type
header and
falls back to utf-8
.
I.e. Content-Length: text/plain; charset=iso-8859-1
would be decoded in latin-1.
pub fn from_read(
reader: impl Read
) -> Self
[src]
pub fn from_read(
reader: impl Read
) -> Self
Create a response from a Read trait impl.
This is hopefully useful for unit tests.
Example:
use std::io::Cursor; let text = "HTTP/1.1 401 Authorization Required\r\n\r\nPlease log in\n"; let read = Cursor::new(text.to_string().into_bytes()); let resp = ureq::Response::from_read(read); assert_eq!(resp.status(), 401);
Trait Implementations
impl Debug for Response
[src]
impl Debug for Response
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
[src]
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter. Read more
impl FromStr for Response
[src]
impl FromStr for Response
type Err = Error
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<Self, Self::Err>
[src]
fn from_str(s: &str) -> Result<Self, Self::Err>
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!!!");
impl Into<Response> for Error
[src]
impl Into<Response> for Error