pub struct Request { /* private fields */ }Expand description
An HTTP request builder and executor with base URL and default headers.
Implementations§
Source§impl Request
impl Request
pub fn with_timeout(timeout_sec: u64) -> Result<Self>
Sourcepub fn set_base_url(&mut self, base_url: &str) -> Result<()>
pub fn set_base_url(&mut self, base_url: &str) -> Result<()>
Set the base URL for all requests.
Sourcepub fn set_default_headers(&mut self, headers: HeaderMap)
pub fn set_default_headers(&mut self, headers: HeaderMap)
Set default headers to be applied on all requests.
Sourcepub async fn get(
&self,
endpoint: &str,
query: Option<Vec<(String, String)>>,
headers: Option<HeaderMap>,
) -> Result<Response>
pub async fn get( &self, endpoint: &str, query: Option<Vec<(String, String)>>, headers: Option<HeaderMap>, ) -> Result<Response>
Send a GET request.
Sourcepub async fn post(
&self,
endpoint: &str,
body: &Value,
headers: Option<HeaderMap>,
) -> Result<Response>
pub async fn post( &self, endpoint: &str, body: &Value, headers: Option<HeaderMap>, ) -> Result<Response>
Send a POST request with JSON body.
Sourcepub async fn put(
&self,
endpoint: &str,
body: &Value,
headers: Option<HeaderMap>,
) -> Result<Response>
pub async fn put( &self, endpoint: &str, body: &Value, headers: Option<HeaderMap>, ) -> Result<Response>
Send a PUT request with JSON body.
Sourcepub async fn delete(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
) -> Result<Response>
pub async fn delete( &self, endpoint: &str, headers: Option<HeaderMap>, ) -> Result<Response>
Send a DELETE request.
Sourcepub async fn post_form(
&self,
endpoint: &str,
form_fields: Vec<FormField>,
headers: Option<HeaderMap>,
) -> Result<Response>
pub async fn post_form( &self, endpoint: &str, form_fields: Vec<FormField>, headers: Option<HeaderMap>, ) -> Result<Response>
Send a POST request with multipart/form-data.
§Arguments
endpoint- The URL endpointform_fields- Vector of form fields (text or file)headers- Optional custom headers
§Important
The Content-Type header will be automatically removed from default and custom headers
to allow reqwest to set the correct multipart/form-data with boundary.
§Example
use toolcraft_request::{FormField, Request};
let client = Request::new()?;
let fields = vec![
FormField::text("name", "John"),
FormField::file("avatar", "/path/to/image.jpg").await?,
];
let response = client.post_form("/upload", fields, None).await?;Sourcepub async fn post_stream(
&self,
endpoint: &str,
body: &Value,
headers: Option<HeaderMap>,
) -> Result<ByteStream>
pub async fn post_stream( &self, endpoint: &str, body: &Value, headers: Option<HeaderMap>, ) -> Result<ByteStream>
Send a streaming POST request and return the response stream.
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
Mutably borrows from an owned value. Read more