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
impl RequestBuilder
Sourcepub fn new(method: Method, uri: Uri) -> Self
pub fn new(method: Method, uri: Uri) -> Self
Create a new builder with the given method and URI.
Sourcepub fn delete(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>
pub fn delete(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>
Create a DELETE request builder.
Sourcepub fn patch(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>
pub fn patch(uri: impl AsRef<str>) -> Result<Self, OxiHttpError>
Create a PATCH request builder.
Sourcepub fn header(
self,
name: impl AsRef<str>,
value: impl AsRef<str>,
) -> Result<Self, OxiHttpError>
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.
Sourcepub fn headers(self, headers: HeaderMap) -> Self
pub fn headers(self, headers: HeaderMap) -> Self
Merge a HeaderMap into the request headers, replacing duplicates.
Sourcepub fn body(self, body: impl Into<Body>) -> Self
pub fn body(self, body: impl Into<Body>) -> Self
Set a raw body (no Content-Type set automatically).
Sourcepub fn json<T: Serialize>(self, value: &T) -> Result<Self, OxiHttpError>
pub fn json<T: Serialize>(self, value: &T) -> Result<Self, OxiHttpError>
Set a JSON body, serialising value and setting Content-Type: application/json.
Sourcepub fn form(self, body: FormBody) -> Self
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.
Sourcepub fn build(self) -> Result<OxiRequest<Body>, OxiHttpError>
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).