Skip to main content

RequestBuilder

Struct RequestBuilder 

Source
pub struct RequestBuilder { /* private fields */ }
Expand description

Execution-free HTTP request builder.

Constructs an http::Request<Body> without performing any network I/O. The terminal method is RequestBuilder::build which returns an OxiRequest<Body>. This is distinct from oxihttp_client::RequestBuilder which owns a network connection and executes requests.

§Example

use oxihttp_core::CoreRequestBuilder;

let req = CoreRequestBuilder::get("http://example.com/api")
    .expect("valid URI")
    .header("x-api-key", "secret")
    .expect("valid header")
    .build()
    .expect("valid request");

assert_eq!(req.method(), http::Method::GET);

Implementations§

Source§

impl RequestBuilder

Source

pub fn new(method: Method, uri: Uri) -> Self

Create a new builder with the given method and URI.

Source

pub fn get(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>

Create a GET request builder.

Source

pub fn post(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>

Create a POST request builder.

Source

pub fn put(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>

Create a PUT request builder.

Source

pub fn delete(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>

Create a DELETE request builder.

Source

pub fn patch(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>

Create a PATCH request builder.

Source

pub fn head(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>

Create a HEAD request builder.

Source

pub fn header( self, name: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<Self, OxiHttpError>

Add a single header, replacing any existing header with the same name.

Source

pub fn headers(self, headers: HeaderMap) -> Self

Merge a HeaderMap into the request headers, replacing duplicates.

Source

pub fn body(self, body: impl Into<Body>) -> Self

Set a raw body (no Content-Type set automatically).

Source

pub fn json<T: Serialize>(self, value: &T) -> Result<Self, OxiHttpError>

Set a JSON body, serialising value and setting Content-Type: application/json.

Source

pub fn form(self, body: FormBody) -> Self

Set a URL-encoded form body, setting Content-Type: application/x-www-form-urlencoded.

Takes a crate::FormBody whose build() is infallible, so this method returns Self directly.

Source

pub fn build(self) -> Result<OxiRequest<Body>, OxiHttpError>

Build the request.

Returns Err only when the accumulated headers, method, or URI are invalid at the http::Request layer (which should be rare given that the builder already validated each piece).

Trait Implementations§

Source§

impl Debug for RequestBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.