Struct CallBody

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

Represents the body of an HTTP request with its content type and schema information.

CallBody encapsulates the raw body data, content type, and schema entry information needed for API requests. It supports various content types including JSON, form-encoded, and raw binary data.

Implementations§

Source§

impl CallBody

Source

pub fn json<T>(t: &T) -> Result<Self, ApiClientError>
where T: Serialize + ToSchema + 'static,

Creates a JSON body from a serializable type.

This method serializes the data as application/json using the serde_json crate. The data must implement Serialize and ToSchema for proper JSON serialization and OpenAPI schema generation.

§Examples
#[derive(Serialize, ToSchema)]
struct User {
    name: String,
    age: u32,
}

let user = User {
    name: "Alice".to_string(),
    age: 30,
};

let body = CallBody::json(&user)?;
Source

pub fn form<T>(t: &T) -> Result<Self, ApiClientError>
where T: Serialize + ToSchema + 'static,

Creates a form-encoded body from a serializable type.

This method serializes the data as application/x-www-form-urlencoded using the serde_urlencoded crate. The data must implement Serialize and ToSchema for proper form encoding and OpenAPI schema generation.

§Examples
#[derive(Serialize, ToSchema)]
struct LoginForm {
    username: String,
    password: String,
}

let form = LoginForm {
    username: "user@example.com".to_string(),
    password: "secret".to_string(),
};

let body = CallBody::form(&form)?;
Source

pub fn raw(data: Vec<u8>, content_type: ContentType) -> Self

Creates a raw body with custom content type.

This method allows you to send arbitrary binary data with a specified content type. This is useful for sending data that doesn’t fit into the standard JSON or form categories.

§Examples
use clawspec_core::CallBody;
use headers::ContentType;

// Send XML data
let xml_data = r#"<?xml version="1.0"?><user><name>John</name></user>"#;
let body = CallBody::raw(
    xml_data.as_bytes().to_vec(),
    ContentType::xml()
);

// Send binary data
let binary_data = vec![0xFF, 0xFE, 0xFD];
let body = CallBody::raw(
    binary_data,
    ContentType::octet_stream()
);
Source

pub fn text(text: &str) -> Self

Creates a text body with text/plain content type.

This is a convenience method for sending plain text data.

§Examples
use clawspec_core::CallBody;

let body = CallBody::text("Hello, World!");
Source

pub fn multipart(parts: Vec<(&str, &str)>) -> Self

Creates a multipart/form-data body for file uploads and form data.

This method creates a multipart body with a generated boundary and supports both text fields and file uploads. The boundary is automatically generated and included in the content type.

§Examples
use clawspec_core::CallBody;

let mut parts = Vec::new();
parts.push(("field1", "value1"));
parts.push(("file", "file content"));

let body = CallBody::multipart(parts);

Trait Implementations§

Source§

impl Clone for CallBody

Source§

fn clone(&self) -> CallBody

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CallBody

Source§

fn fmt(&self, __derive_more_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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,