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]
impl Request
pub fn build(&self) -> Request
[src]
pub fn build(&self) -> Request
"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]
pub fn call(&mut self) -> Response
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_string<S>(&mut self, data: S) -> Response where
S: Into<String>,
[src]
pub fn send_string<S>(&mut self, data: S) -> Response where
S: Into<String>,
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(
&mut self,
reader: impl Read + 'static
) -> Response
[src]
pub fn send(
&mut self,
reader: impl Read + 'static
) -> Response
Send data from a reader.
The Content-Length
header is not set because we can't know the length of the reader.
use std::io::Cursor; let text = "Hello there!\n"; let read = Cursor::new(text.to_string().into_bytes()); let resp = ureq::post("/somewhere") .set("Content-Type", "text/plain") .send(read);
pub fn set<K, V>(&mut self, header: K, value: V) -> &mut Request where
K: Into<String>,
V: Into<String>,
[src]
pub fn set<K, V>(&mut self, header: K, value: V) -> &mut Request where
K: Into<String>,
V: Into<String>,
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<'a>(&self, name: &'a str) -> Option<&str>
[src]
pub fn header<'a>(&self, name: &'a str) -> Option<&str>
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 has<'a>(&self, name: &'a str) -> bool
[src]
pub fn has<'a>(&self, name: &'a str) -> bool
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<'a>(&self, name: &'a str) -> Vec<&str>
[src]
pub fn all<'a>(&self, name: &'a str) -> Vec<&str>
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 set_map<K, V, I>(&mut self, headers: I) -> &mut Request where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
[src]
pub fn set_map<K, V, I>(&mut self, headers: I) -> &mut Request where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
Set many headers.
#[macro_use] extern crate ureq; fn main() { let r = ureq::get("/my_page") .set_map(map! { "X-API-Key" => "foobar", "Accept" => "text/plain" }) .call(); if r.ok() { println!("yay got {}", r.into_string().unwrap()); } }
pub fn query<K, V>(&mut self, param: K, value: V) -> &mut Request where
K: Into<String>,
V: Into<String>,
[src]
pub fn query<K, V>(&mut self, param: K, value: V) -> &mut Request where
K: Into<String>,
V: Into<String>,
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_map<K, V, I>(&mut self, params: I) -> &mut Request where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
[src]
pub fn query_map<K, V, I>(&mut self, params: I) -> &mut Request where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
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); }
pub fn query_str<S>(&mut self, query: S) -> &mut Request where
S: Into<String>,
[src]
pub fn query_str<S>(&mut self, query: S) -> &mut Request where
S: Into<String>,
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]
pub fn timeout_connect(&mut self, millis: u64) -> &mut Request
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_connect(1_000) // wait max 1 second to connect .call(); println!("{:?}", r);
pub fn timeout_read(&mut self, millis: u64) -> &mut Request
[src]
pub fn timeout_read(&mut self, millis: u64) -> &mut Request
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);
pub fn timeout_write(&mut self, millis: u64) -> &mut Request
[src]
pub fn timeout_write(&mut self, millis: u64) -> &mut Request
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);
pub fn auth<S, T>(&mut self, user: S, pass: T) -> &mut Request where
S: Into<String>,
T: Into<String>,
[src]
pub fn auth<S, T>(&mut self, user: S, pass: T) -> &mut Request where
S: Into<String>,
T: Into<String>,
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<S, T>(&mut self, kind: S, pass: T) -> &mut Request where
S: Into<String>,
T: Into<String>,
[src]
pub fn auth_kind<S, T>(&mut self, kind: S, pass: T) -> &mut Request where
S: Into<String>,
T: Into<String>,
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]
pub fn redirects(&mut self, n: u32) -> &mut Request
How many redirects to follow.
Defaults to 5
.
let r = ureq::get("/my_page") .redirects(10) .call(); println!("{:?}", r);
pub fn get_method(&self) -> &str
[src]
pub fn get_method(&self) -> &str
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]
pub fn get_url(&self) -> &str
Get the url this request was created with.
This value is not normalized, it is exactly as set.
Example:
let req = ureq::post("https://cool.server/innit") .build(); assert_eq!(req.get_url(), "https://cool.server/innit");
Trait Implementations
impl Clone for Request
[src]
impl Clone for Request
fn clone(&self) -> Request
[src]
fn clone(&self) -> Request
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Default for Request
[src]
impl Default for Request
impl Debug for Request
[src]
impl Debug for Request