Skip to main content

Client

Struct Client 

Source
pub struct Client<'a> { /* private fields */ }
Expand description

Builder for constructing and sending a blocking HTTP request.

Defaults to GET when created via Default.

Implementations§

Source§

impl<'a> Client<'a>

Source

pub fn new() -> Self

Creates a new builder with default settings (GET, no headers, no query).

Source

pub fn with_user_agent(agent: impl Into<Cow<'a, str>>) -> Self

Creates a new builder with a default User-Agent header.

The User-Agent is only applied if the request does not already set one.

Source

pub fn max_redirects(self, max: i8) -> Self

Sets the number of redirects to follow.

Setting -1 means unlimited responses.

§Examples
let resp = curl_rest::Client::default()
    .get()
    .max_redirects(-1)
    .send("https://example.com/private")?;
§Errors

This method does not return errors. Header validation happens in send.

Source

pub fn brotli(self, is_enabled: bool) -> Self

Sets brotli on or off. This setting interferes with other compression algorithms like gzip. To use those, leave this as false.

This has to be set to true to disable automatic decompression because libcurl does not support brotli.

Source

pub fn method(self, method: Method) -> Self

Sets the HTTP method explicitly.

Source

pub fn get(self) -> Self

Sets the request method to GET.

Source

pub fn post(self) -> Self

Sets the request method to POST.

Source

pub fn put(self) -> Self

Sets the request method to PUT.

Source

pub fn delete(self) -> Self

Sets the request method to DELETE.

Source

pub fn head(self) -> Self

Sets the request method to HEAD.

Source

pub fn options(self) -> Self

Sets the request method to OPTIONS.

Source

pub fn patch(self) -> Self

Sets the request method to PATCH.

Source

pub fn connect(self) -> Self

Sets the request method to CONNECT.

Source

pub fn trace(self) -> Self

Sets the request method to TRACE.

Source

pub fn header(self, header: Header<'a>) -> Self

Adds a single header.

§Examples
let resp = curl_rest::Client::default()
    .get()
    .header(curl_rest::Header::Authorization("Bearer token".into()))
    .send("https://example.com/private")?;
§Errors

This method does not return errors. Header validation happens in send.

Source

pub fn headers<I>(self, headers: I) -> Self
where I: IntoIterator<Item = Header<'a>>,

Adds multiple headers.

§Examples
let resp = curl_rest::Client::default()
    .get()
    .headers([
        curl_rest::Header::Accept("application/json".into()),
        curl_rest::Header::UserAgent("curl-rest/0.1".into()),
    ])
    .send("https://example.com/users")?;
§Errors

This method does not return errors. Header validation happens in send.

Source

pub fn query_param(self, param: QueryParam<'a>) -> Self

Adds a single query parameter.

§Examples
let resp = curl_rest::Client::default()
    .get()
    .query_param(curl_rest::QueryParam::new("q", "rust"))
    .send("https://example.com/search")?;
§Errors

This method does not return errors. URL validation happens in send.

Source

pub fn query_param_kv( self, key: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> Self

Adds a single query parameter by key/value.

§Examples
let resp = curl_rest::Client::default()
    .get()
    .query_param_kv("page", "1")
    .send("https://example.com/search")?;
§Errors

This method does not return errors. URL validation happens in send.

Source

pub fn query_params<I>(self, params: I) -> Self
where I: IntoIterator<Item = QueryParam<'a>>,

Adds multiple query parameters.

§Examples
let resp = curl_rest::Client::default()
    .get()
    .query_params([
        curl_rest::QueryParam::new("sort", "desc"),
        curl_rest::QueryParam::new("limit", "50"),
    ])
    .send("https://example.com/items")?;
§Errors

This method does not return errors. URL validation happens in send.

Source

pub fn body(self, body: Body<'a>) -> Self

Sets a request body explicitly.

§Examples
let resp = curl_rest::Client::default()
    .post()
    .body(curl_rest::Body::Text("hello".into()))
    .send("https://example.com/echo")?;
§Errors

This method does not return errors. Failures are reported by send.

Source

pub fn body_bytes(self, bytes: impl Into<Cow<'a, [u8]>>) -> Self

Sets a raw byte body.

§Examples
let resp = curl_rest::Client::default()
    .post()
    .body_bytes(vec![1, 2, 3])
    .send("https://example.com/bytes")?;
§Errors

This method does not return errors. Failures are reported by send.

Source

pub fn body_text(self, text: impl Into<Cow<'a, str>>) -> Self

Sets a text body with a text/plain; charset=utf-8 default content type.

§Examples
let resp = curl_rest::Client::default()
    .post()
    .body_text("hello")
    .send("https://example.com/echo")?;
§Errors

This method does not return errors. Failures are reported by send.

Source

pub fn body_json(self, json: impl Into<Cow<'a, str>>) -> Self

Sets a JSON body with an application/json default content type.

§Examples
let resp = curl_rest::Client::default()
    .post()
    .body_json(r#"{"name":"stanley"}"#)
    .send("https://example.com/users")?;
§Errors

This method does not return errors. Failures are reported by send.

Source

pub fn send(self, url: &str) -> Result<Response, Error>

Sends the request to the provided URL.

§Errors

Returns an error if the URL is invalid, a header name or value is malformed, the status code is unrecognized, or libcurl reports a failure.

Trait Implementations§

Source§

impl<'a> Default for Client<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Client<'a>

§

impl<'a> RefUnwindSafe for Client<'a>

§

impl<'a> Send for Client<'a>

§

impl<'a> Sync for Client<'a>

§

impl<'a> Unpin for Client<'a>

§

impl<'a> UnsafeUnpin for Client<'a>

§

impl<'a> UnwindSafe for Client<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.