Struct ureq::Request[][src]

pub struct Request { /* fields omitted */ }

Request instances are builders that creates a request.

let mut request = ureq::get("https://www.google.com/");

let response = request
    .query("foo", "bar baz") // add ?foo=bar%20baz
    .call();                 // run the request

Implementations

impl Request[src]

pub fn build(&self) -> Request[src]

“Builds” this request which is effectively the same as cloning. This is needed when we use a chain of request builders, but don’t want to send the request at the end of the chain.

let r = ureq::get("/my_page")
    .set("X-Foo-Bar", "Baz")
    .build();

pub fn call(&mut self) -> Response[src]

Executes the request and blocks the caller until done.

Use .timeout_connect() and .timeout_read() to avoid blocking forever.

let r = ureq::get("/my_page")
    .timeout_connect(10_000) // max 10 seconds
    .call();

println!("{:?}", r);

pub fn send_json(&mut self, data: SerdeValue) -> Response[src]

Send data a json value.

Requires feature ureq = { version = "*", features = ["json"] }

The Content-Length header is implicitly set to the length of the serialized value.

#[macro_use]
extern crate ureq;

fn main() {
let r = ureq::post("/my_page")
    .send_json(json!({ "name": "martin", "rust": true }));
println!("{:?}", r);
}

pub fn send_bytes(&mut self, data: &[u8]) -> Response[src]

Send data as bytes.

The Content-Length header is implicitly set to the length of the serialized value.

let body = b"Hello world!";
let r = ureq::post("/my_page")
    .send_bytes(body);
println!("{:?}", r);

pub fn send_string(&mut self, data: &str) -> Response[src]

Send data as a string.

The Content-Length header is implicitly set to the length of the serialized value. Defaults to utf-8

Charset support

Requires feature ureq = { version = "*", features = ["charset"] }

If a Content-Type header is present and it contains a charset specification, we attempt to encode the string using that character set. If it fails, we fall back on utf-8.

// this example requires features = ["charset"]

let r = ureq::post("/my_page")
    .set("Content-Type", "text/plain; charset=iso-8859-1")
    .send_string("Hällo Wörld!");
println!("{:?}", r);

pub fn send_form(&mut self, data: &[(&str, &str)]) -> Response[src]

Send a sequence of (key, value) pairs as form-urlencoded data.

The Content-Type header is implicitly set to application/x-www-form-urlencoded. The Content-Length header is implicitly set to the length of the serialized value.

#[macro_use]
extern crate ureq;

fn main() {
let r = ureq::post("/my_page")
    .send_form(&[("foo", "bar"),("foo2", "bar2")]);
println!("{:?}", r);
}

pub fn send(&mut self, reader: impl Read) -> Response[src]

Send data from a reader.

If no Content-Length and Transfer-Encoding header has been set, it uses the chunked transfer encoding.

The caller may set the Content-Length header to the expected byte size of the reader if is known.

The input from the reader is buffered into chunks of size 16,384, the max size of a TLS fragment.

use std::io::Cursor;

let read = Cursor::new(vec![0x20; 100_000]);

let resp = ureq::post("http://localhost/example-upload")
    .set("Content-Type", "text/plain")
    .send(read);

pub fn set(&mut self, header: &str, value: &str) -> &mut Request[src]

Set a header field.

let r = ureq::get("/my_page")
    .set("X-API-Key", "foobar")
    .set("Accept", "text/plain")
    .call();

 if r.ok() {
     println!("yay got {}", r.into_string().unwrap());
 } else {
     println!("Oh no error!");
 }

pub fn header(&self, name: &str) -> Option<&str>[src]

Returns the value for a set header.

let req = ureq::get("/my_page")
    .set("X-API-Key", "foobar")
    .build();
assert_eq!("foobar", req.header("x-api-Key").unwrap());

pub fn header_names(&self) -> Vec<String>[src]

A list of the set header names in this request. Lowercased to be uniform.

let req = ureq::get("/my_page")
    .set("X-API-Key", "foobar")
    .set("Content-Type", "application/json")
    .build();
assert_eq!(req.header_names(), vec!["x-api-key", "content-type"]);

pub fn has(&self, name: &str) -> bool[src]

Tells if the header has been set.

let req = ureq::get("/my_page")
    .set("X-API-Key", "foobar")
    .build();
assert_eq!(true, req.has("x-api-Key"));

pub fn all(&self, name: &str) -> Vec<&str>[src]

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

let req = ureq::get("/my_page")
    .set("X-Forwarded-For", "1.2.3.4")
    .set("X-Forwarded-For", "2.3.4.5")
    .build();
assert_eq!(req.all("x-forwarded-for"), vec![
    "1.2.3.4",
    "2.3.4.5",
]);

pub fn query(&mut self, param: &str, value: &str) -> &mut Request[src]

Set a query parameter.

For example, to set ?format=json&dest=/login

let r = ureq::get("/my_page")
    .query("format", "json")
    .query("dest", "/login")
    .call();

println!("{:?}", r);

pub fn query_str(&mut self, query: &str) -> &mut Request[src]

Set query parameters as a string.

For example, to set ?format=json&dest=/login

let r = ureq::get("/my_page")
    .query_str("?format=json&dest=/login")
    .call();
println!("{:?}", r);

pub fn timeout_connect(&mut self, millis: u64) -> &mut Request[src]

Timeout for the socket connection to be successful. If both this and .timeout() are both set, .timeout_connect() takes precedence.

The default is 0, which means a request can block forever.

let r = ureq::get("/my_page")
    .timeout_connect(1_000) // wait max 1 second to connect
    .call();
println!("{:?}", r);

pub fn timeout_read(&mut self, millis: u64) -> &mut Request[src]

Timeout for the individual reads of the socket. If both this and .timeout() are both set, .timeout() takes precedence.

The default is 0, which means it can block forever.

let r = ureq::get("/my_page")
    .timeout_read(1_000) // wait max 1 second for the read
    .call();
println!("{:?}", r);

pub fn timeout_write(&mut self, millis: u64) -> &mut Request[src]

Timeout for the individual writes to the socket. If both this and .timeout() are both set, .timeout() takes precedence.

The default is 0, which means it can block forever.

let r = ureq::get("/my_page")
    .timeout_write(1_000)   // wait max 1 second for sending.
    .call();
println!("{:?}", r);

pub fn timeout(&mut self, timeout: Duration) -> &mut Request[src]

Timeout for the overall request, including DNS resolution, connection time, redirects, and reading the response body. Slow DNS resolution may cause a request to exceed the timeout, because the DNS request cannot be interrupted with the available APIs.

This takes precedence over .timeout_read() and .timeout_write(), but not .timeout_connect().

// wait max 1 second for whole request to complete.
let r = ureq::get("/my_page")
    .timeout(std::time::Duration::from_secs(1))
    .call();
println!("{:?}", r);

pub fn auth(&mut self, user: &str, pass: &str) -> &mut Request[src]

Basic auth.

These are the same

let r1 = ureq::get("http://localhost/my_page")
    .auth("martin", "rubbermashgum")
    .call();
 println!("{:?}", r1);

let r2 = ureq::get("http://martin:rubbermashgum@localhost/my_page").call();
println!("{:?}", r2);

pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Request[src]

Auth of other kinds such as Digest, Token etc.

let r = ureq::get("http://localhost/my_page")
    .auth_kind("token", "secret")
    .call();
println!("{:?}", r);

pub fn redirects(&mut self, n: u32) -> &mut Request[src]

How many redirects to follow.

Defaults to 5. Set to 0 to avoid redirects and instead get a response object with the 3xx status code.

If the redirect count hits this limit (and it’s > 0), a synthetic 500 error response is produced.

let r = ureq::get("/my_page")
    .redirects(10)
    .call();
println!("{:?}", r);

pub fn get_method(&self) -> &str[src]

Get the method this request is using.

Example:

let req = ureq::post("/somewhere")
    .build();
assert_eq!(req.get_method(), "POST");

pub fn get_url(&self) -> &str[src]

Get the url this request was created with.

This value is not normalized, it is exactly as set. It does not contain any added query parameters.

Example:

let req = ureq::post("https://cool.server/innit")
    .build();
assert_eq!(req.get_url(), "https://cool.server/innit");

pub fn get_host(&self) -> Result<String, Error>[src]

Normalizes and returns the host that will be used for this request.

Example:

let req1 = ureq::post("https://cool.server/innit")
    .build();
assert_eq!(req1.get_host().unwrap(), "cool.server");

let req2 = ureq::post("http://localhost/some/path")
    .build();
assert_eq!(req2.get_host().unwrap(), "localhost");

pub fn get_scheme(&self) -> Result<String, Error>[src]

Returns the scheme for this request.

Example:

let req = ureq::post("https://cool.server/innit")
    .build();
assert_eq!(req.get_scheme().unwrap(), "https");

pub fn get_query(&self) -> Result<String, Error>[src]

The complete query for this request.

Example:

let req = ureq::post("https://cool.server/innit?foo=bar")
    .query("format", "json")
    .build();
assert_eq!(req.get_query().unwrap(), "?foo=bar&format=json");

pub fn get_path(&self) -> Result<String, Error>[src]

The normalized url of this request.

Example:

let req = ureq::post("https://cool.server/innit")
    .build();
assert_eq!(req.get_path().unwrap(), "/innit");

pub fn set_proxy(&mut self, proxy: Proxy) -> &mut Request[src]

Set the proxy server to use for the connection.

Example:

let proxy = ureq::Proxy::new("user:password@cool.proxy:9090").unwrap();
let req = ureq::post("https://cool.server")
    .set_proxy(proxy)
    .build();

pub fn set_tls_config(&mut self, tls_config: Arc<ClientConfig>) -> &mut Request[src]

Set the TLS client config to use for the connection. See ClientConfig.

See ClientConfig.

Example:

let tls_config = std::sync::Arc::new(rustls::ClientConfig::new());
let req = ureq::post("https://cool.server")
    .set_tls_config(tls_config.clone());

Trait Implementations

impl Clone for Request[src]

impl Debug for Request[src]

impl Default for Request[src]

Auto Trait Implementations

impl !RefUnwindSafe for Request

impl Send for Request

impl Sync for Request

impl Unpin for Request

impl !UnwindSafe for Request

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.