pub struct Request {
pub method: Method,
pub url: Url,
pub headers: Headers,
pub body: Option<Vec<u8>>,
}
Fields§
§method: Method
§url: Url
§headers: Headers
§body: Option<Vec<u8>>
Implementations§
source§impl Request
impl Request
sourcepub fn new(method: Method, url: Url) -> Self
pub fn new(method: Method, url: Url) -> Self
Construct a new request to the given url
using the given method
.
Note that the request is not made until send()
is called.
pub fn send(self) -> Result<Response, Error>
sourcepub fn query(self, pairs: &[(&str, &str)]) -> Self
pub fn query(self, pairs: &[(&str, &str)]) -> Self
Append the provided query parameters to the URL
§Example
let some_url = url::Url::parse("https://www.example.com/xyz").unwrap();
let req = Request::post(some_url).query(&[("a", "1234"), ("b", "qwerty")]);
assert_eq!(req.url.as_str(), "https://www.example.com/xyz?a=1234&b=qwerty");
// This appends to the query query instead of replacing `a`.
let req = req.query(&[("a", "5678")]);
assert_eq!(req.url.as_str(), "https://www.example.com/xyz?a=1234&b=qwerty&a=5678");
sourcepub fn set_query<'a, Q: Into<Option<&'a str>>>(self, query: Q) -> Self
pub fn set_query<'a, Q: Into<Option<&'a str>>>(self, query: Q) -> Self
Set the query string of the URL. Note that req.set_query(None)
will
clear the query.
See also Request::query
which appends a slice of query pairs, which is
typically more ergonomic when usable.
§Example
let some_url = url::Url::parse("https://www.example.com/xyz").unwrap();
let req = Request::post(some_url).set_query("a=b&c=d");
assert_eq!(req.url.as_str(), "https://www.example.com/xyz?a=b&c=d");
let req = req.set_query(None);
assert_eq!(req.url.as_str(), "https://www.example.com/xyz");
sourcepub fn headers<I>(self, to_add: I) -> Selfwhere
I: IntoIterator<Item = Header>,
pub fn headers<I>(self, to_add: I) -> Selfwhere
I: IntoIterator<Item = Header>,
Add all the provided headers to the list of headers to send with this request.
sourcepub fn header<Name, Val>(self, name: Name, val: Val) -> Result<Self, Error>
pub fn header<Name, Val>(self, name: Name, val: Val) -> Result<Self, Error>
Add the provided header to the list of headers to send with this request.
This returns Err
if val
contains characters that may not appear in
the body of a header.
§Example
Request::post(some_url)
.header(header_names::CONTENT_TYPE, "application/json")?
.header("My-Header", "Some special value")?;
// ...
sourcepub fn json<T: ?Sized + Serialize>(self, val: &T) -> Self
pub fn json<T: ?Sized + Serialize>(self, val: &T) -> Self
Set body to the result of serializing val
, and, unless it has already
been set, set the Content-Type header to “application/json”.
Note: This panics if serde_json::to_vec fails. This can only happen in a couple cases:
- Trying to serialize a map with non-string keys.
- We wrote a custom serializer that fails.
Neither of these are things we do. If they happen, it seems better for
this to fail hard with an easy to track down panic, than for e.g. sync
to fail with a JSON parse error (which we’d probably attribute to
corrupt data on the server, or something).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Request
impl RefUnwindSafe for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl UnwindSafe for Request
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)