pub struct Request { /* private fields */ }
Expand description
An HTTP request, returns a Response
.
Implementations§
source§impl Request
impl Request
sourcepub fn new(method: Method, url: Url) -> Self
pub fn new(method: Method, url: Url) -> Self
Create a new instance.
This method is particularly useful when input URLs might be passed by third parties, and you don’t want to panic if they’re malformed. If URLs are statically encoded, it might be easier to use one of the shorthand methods instead.
§Examples
fn main() -> crux_http::Result<()> {
use crux_http::http::{Url, Method};
let url = Url::parse("https://httpbin.org/get")?;
let req = crux_http::Request::new(Method::Get, url);
sourcepub fn query<T: DeserializeOwned>(&self) -> Result<T>
pub fn query<T: DeserializeOwned>(&self) -> Result<T>
Get the URL querystring.
§Examples
#[derive(Serialize, Deserialize)]
struct Index {
page: u32
}
let req = caps.http.get("https://httpbin.org/get?page=2").build();
let Index { page } = req.query()?;
assert_eq!(page, 2);
sourcepub fn set_query(&mut self, query: &impl Serialize) -> Result<()>
pub fn set_query(&mut self, query: &impl Serialize) -> Result<()>
Set the URL querystring.
§Examples
#[derive(Serialize, Deserialize)]
struct Index {
page: u32
}
let query = Index { page: 2 };
let mut req = caps.http.get("https://httpbin.org/get").build();
req.set_query(&query)?;
assert_eq!(req.url().query(), Some("page=2"));
assert_eq!(req.url().as_str(), "https://httpbin.org/get?page=2");
sourcepub fn header(&self, key: impl Into<HeaderName>) -> Option<&HeaderValues>
pub fn header(&self, key: impl Into<HeaderName>) -> Option<&HeaderValues>
Get an HTTP header.
§Examples
let mut req = caps.http.get("https://httpbin.org/get").build();
req.set_header("X-Requested-With", "surf");
assert_eq!(req.header("X-Requested-With").unwrap(), "surf");
sourcepub fn header_mut(
&mut self,
name: impl Into<HeaderName>,
) -> Option<&mut HeaderValues>
pub fn header_mut( &mut self, name: impl Into<HeaderName>, ) -> Option<&mut HeaderValues>
Get a mutable reference to a header.
sourcepub fn insert_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> Option<HeaderValues>
pub fn insert_header( &mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues, ) -> Option<HeaderValues>
Set an HTTP header.
sourcepub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
)
pub fn append_header( &mut self, name: impl Into<HeaderName>, values: impl ToHeaderValues, )
Append a header to the headers.
Unlike insert
this function will not override the contents of a header, but insert a
header if there aren’t any. Or else append to the existing list of headers.
sourcepub fn remove_header(
&mut self,
name: impl Into<HeaderName>,
) -> Option<HeaderValues>
pub fn remove_header( &mut self, name: impl Into<HeaderName>, ) -> Option<HeaderValues>
Remove a header.
sourcepub fn iter_mut(&mut self) -> IterMut<'_>
pub fn iter_mut(&mut self) -> IterMut<'_>
An iterator visiting all header pairs in arbitrary order, with mutable references to the values.
sourcepub fn header_names(&self) -> Names<'_>
pub fn header_names(&self) -> Names<'_>
An iterator visiting all header names in arbitrary order.
sourcepub fn header_values(&self) -> Values<'_>
pub fn header_values(&self) -> Values<'_>
An iterator visiting all header values in arbitrary order.
sourcepub fn set_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
)
pub fn set_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )
Set an HTTP header.
§Examples
let mut req = caps.http.get("https://httpbin.org/get").build();
req.set_header("X-Requested-With", "surf");
assert_eq!(req.header("X-Requested-With").unwrap(), "surf");
sourcepub fn set_ext<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T>
pub fn set_ext<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T>
Set a request extension value.
sourcepub fn method(&self) -> Method
pub fn method(&self) -> Method
Get the request HTTP method.
§Examples
let req = caps.http.get("https://httpbin.org/get").build();
assert_eq!(req.method(), crux_http::http::Method::Get);
sourcepub fn url(&self) -> &Url
pub fn url(&self) -> &Url
Get the request url.
§Examples
use crux_http::http::Url;
let req = caps.http.get("https://httpbin.org/get").build();
assert_eq!(req.url(), &Url::parse("https://httpbin.org/get")?);
sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
Get the request content type as a Mime
.
Gets the Content-Type
header and parses it to a Mime
type.
§Panics
This method will panic if an invalid MIME type was set as a header. Use the set_header
method to bypass any checks.
sourcepub fn set_content_type(&mut self, mime: Mime)
pub fn set_content_type(&mut self, mime: Mime)
Set the request content type from a Mime
.
sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
Get the length of the body stream, if it has been set.
This value is set when passing a fixed-size object into as the body.
E.g. a string, or a buffer. Consumers of this API should check this
value to decide whether to use Chunked
encoding, or set the
response length.
sourcepub fn is_empty(&self) -> Option<bool>
pub fn is_empty(&self) -> Option<bool>
Returns true
if the set length of the body stream is zero, false
otherwise.
sourcepub fn set_body(&mut self, body: impl Into<Body>)
pub fn set_body(&mut self, body: impl Into<Body>)
Pass an AsyncRead
stream as the request body.
§Mime
The encoding is set to application/octet-stream
.
sourcepub fn take_body(&mut self) -> Body
pub fn take_body(&mut self) -> Body
Take the request body as a Body
.
This method can be called after the body has already been taken or read,
but will return an empty Body
.
This is useful for consuming the body via an AsyncReader or AsyncBufReader.
sourcepub fn body_string(&mut self, string: String)
pub fn body_string(&mut self, string: String)
sourcepub fn body_bytes(&mut self, bytes: impl AsRef<[u8]>)
pub fn body_bytes(&mut self, bytes: impl AsRef<[u8]>)
sourcepub fn middleware(&mut self, middleware: impl Middleware)
pub fn middleware(&mut self, middleware: impl Middleware)
Push middleware onto a per-request middleware stack.
Important: Setting per-request middleware incurs extra allocations.
Creating a Client
with middleware is recommended.
Client middleware is run before per-request middleware.
See the middleware submodule for more information on middleware.
§Examples
let mut req = caps.http.get("https://httpbin.org/get").build();
req.middleware(crux_http::middleware::Redirect::default());
Trait Implementations§
source§impl<'a> IntoIterator for &'a Request
impl<'a> IntoIterator for &'a Request
source§impl<'a> IntoIterator for &'a mut Request
impl<'a> IntoIterator for &'a mut Request
source§impl IntoIterator for Request
impl IntoIterator for Request
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
)