Struct ureq::Request[][src]

pub struct Request { /* fields omitted */ }
Expand description

Request instances are builders that creates a request.

let response = ureq::get("http://example.com/form")
    .query("foo", "bar baz")  // add ?foo=bar+baz
    .call()?;                 // run the request

Implementations

Sets overall timeout for the request, overriding agent’s configuration if any.

Sends the request with no body and blocks the caller until done.

Use this with GET, HEAD, OPTIONS or TRACE. It sends neither Content-Length nor Transfer-Encoding.

let resp = ureq::get("http://example.com/")
    .call()?;

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.

let resp = ureq::post("http://httpbin.org/post")
    .send_json(ureq::json!({
      "name": "martin",
      "rust": true,
    }))?;

Send data as bytes.

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

let resp = ureq::put("http://httpbin.org/put")
    .send_bytes(&[0; 1000])?;

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 resp = ureq::post("http://httpbin.org/post")
    .set("Content-Type", "text/plain; charset=iso-8859-1")
    .send_string("Hällo Wörld!")?;

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.

let resp = ureq::post("http://httpbin.org/post")
    .send_form(&[
      ("foo", "bar"),
      ("foo2", "bar2"),
    ])?;

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]);
let resp = ureq::post("http://httpbin.org/post")
    .send(read)?;

Set a header field.

let resp = ureq::get("http://httpbin.org/bytes/1000")
    .set("Accept", "text/plain")
    .set("Range", "bytes=500-999")
    .call()?;

Returns the value for a set header.

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

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");
assert_eq!(req.header_names(), vec!["x-api-key", "content-type"]);

Tells if the header has been set.

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

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

assert_eq!(req.all("x-forwarded-for"), vec![
    "1.2.3.4",
    "2.3.4.5",
]);

Set a query parameter.

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

let resp = ureq::get("http://httpbin.org/get")
    .query("format", "json")
    .query("dest", "/login")
    .call()?;

Returns the value of the request method. Something like GET, POST, PUT etc.

let req = ureq::put("http://httpbin.org/put");

assert_eq!(req.method(), "PUT");

Get the url str that will be used for this request.

The url might differ from that originally provided when constructing the request if additional query parameters have been added using Request::query().

In case the original url provided to build the request is not possible to parse to a Url, this function returns the original, and it will error once the Request object is used.

let req = ureq::get("http://httpbin.org/get")
    .query("foo", "bar");

assert_eq!(req.url(), "http://httpbin.org/get?foo=bar");
let req = ureq::get("SO WRONG")
    .query("foo", "bar"); // does nothing

assert_eq!(req.url(), "SO WRONG");

Get the parsed url that will be used for this request. The parsed url has functions to inspect the parts of the url further.

The url might differ from that originally provided when constructing the request if additional query parameters have been added using Request::query().

Returns a Result since a common use case is to construct the Request using a &str in which case the url needs to be parsed to inspect the parts. If the Request url is not possible to parse, this function produces the same error that would otherwise happen when call or send_* is called.

let req = ureq::get("http://httpbin.org/get")
    .query("foo", "bar");

assert_eq!(req.request_url()?.host(), "httpbin.org");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

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.