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]

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

The entire status line like: HTTP/1.1 200 OK

The http version: HTTP/1.1

The status as a u16: 200

The status text: OK

The header corresponding header value for the give name, if any.

Tells if the response has the named header.

Important traits for Vec<u8>

All headers corresponding values for the give name, or empty vector.

Whether the response status is: 200 <= status <= 299

Whether the response status is: 400 <= status <= 499

Whether the response status is: 500 <= status <= 599

Whether the response status is: 400 <= status <= 599

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

Get the actual underlying error when the response is "synthetic".

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

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

Turn this response into a impl Read of the body.

  1. If "Transfer-Encoding: chunked", the returned reader will unchunk it and any "Content-Length" header is ignored.
  2. If "Content-Length" is set, the returned reader is limited to this byte length regardless of how many bytes the server sends.
  3. If no length header, the reader is until server stream end.

Example:

use std::io::Read;

let resp =
    ureq::get("https://raw.githubusercontent.com/algesten/ureq/master/.gitignore").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);

Turn this response into a String of the response body. Attempts to respect the character encoding of the "Content-Type" and falls back to utf-8.

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://raw.githubusercontent.com/algesten/ureq/master/.gitignore").call();

let text = resp.into_string().unwrap();

assert!(text.contains("target"));

Turn this response into a (serde) JSON value of the response body.

Example:

let resp =
    ureq::get("https://raw.githubusercontent.com/algesten/ureq/master/src/test/hello_world.json").call();

let json = resp.into_json().unwrap();

assert_eq!(json["hello"], "world");

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]

Formats the value using the given formatter. Read more

impl FromStr for Response
[src]

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

impl Into<Response> for Error
[src]

Performs the conversion.

Auto Trait Implementations

impl Send for Response

impl Sync for Response