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
impl CallBody
Sourcepub fn json<T>(t: &T) -> Result<Self, ApiClientError>
pub fn json<T>(t: &T) -> Result<Self, ApiClientError>
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)?;
Sourcepub fn form<T>(t: &T) -> Result<Self, ApiClientError>
pub fn form<T>(t: &T) -> Result<Self, ApiClientError>
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)?;
Sourcepub fn raw(data: Vec<u8>, content_type: ContentType) -> Self
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()
);
Sourcepub fn text(text: &str) -> Self
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!");
Sourcepub fn multipart(parts: Vec<(&str, &str)>) -> Self
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);