Struct ureq::Response

source ·
pub struct Response { /* private fields */ }
Expand description

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.

Implementations§

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 URL we ended up at. This can differ from the request url when we have followed redirects.

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.

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

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

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.

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§

Formats the value using the given formatter. Read more

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!!!");
The associated error which can be returned from parsing.
Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.