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

Methods

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

Executes the request and blocks the caller until done.

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

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

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

Send data a json value.

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

Send data as a string.

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

let r = ureq::post("/my_page")
    .set("Content-Type", "text/plain")
    .send_string("Hello World!");
println!("{:?}", r);

Send data from a reader.

The Content-Length header is not set because we can't know the length of the reader.

Set a header field.

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

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

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

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

Important traits for Vec<u8>

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",
]);

Set many headers.

#[macro_use]
extern crate ureq;

fn main() {
let r = ureq::get("/my_page")
    .set_map(map! {
        "X-API-Key" => "foobar",
        "Accept" => "application/json"
    })
    .call();

if r.ok() {
    println!("yay got {}", r.into_json().unwrap());
}
}

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

Set many query parameters.

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

#[macro_use]
extern crate ureq;

fn main() {
let r = ureq::get("/my_page")
    .query_map(map! {
        "format" => "json",
        "dest" => "/login"
    })
    .call();

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

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

Timeout for the socket connection to be successful.

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

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

Timeout for the individual reads of the socket.

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

Timeout for the individual writes to the socket.

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

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

Auth of other kinds such as Digest, Token etc.

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

How many redirects to follow.

Defaults to 5.

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

Trait Implementations

impl Clone for Request
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Default for Request
[src]

Returns the "default value" for a type. Read more

impl Debug for Request
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Request

impl Sync for Request