pub struct Body { /* private fields */ }Expand description
A request body that can be raw bytes or serialized JSON.
Use the From impls to pass raw bytes, or Body::json to serialize a
value and automatically set the Content-Type: application/json header.
§Examples
use winhttp::Body;
// Raw bytes — no Content-Type is set.
let body: Body = b"hello".as_slice().into();
let body: Body = vec![1, 2, 3].into();
let body: Body = "plain text".into();use winhttp::Body;
// JSON — Content-Type is set to application/json.
let body = Body::json(&serde_json::json!({"key": "value"}))?;Implementations§
Source§impl Body
impl Body
Sourcepub fn json<T: Serialize>(value: &T) -> Result<Self>
Available on crate feature json only.
pub fn json<T: Serialize>(value: &T) -> Result<Self>
json only.Serialize value as JSON and set Content-Type: application/json.
Returns a windows::core::Error if serialization fails.
Requires the json feature.
§Example
use winhttp::{Body, Client};
use serde::Serialize;
#[derive(Serialize)]
struct Payload { name: String }
let client = Client::new()?;
let resp = client.post("/users", Body::json(&Payload { name: "Ada".into() })?)?;Sourcepub fn content_type(&self) -> Option<&'static str>
pub fn content_type(&self) -> Option<&'static str>
Returns the content type that should be set for this body, if any.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Body
impl RefUnwindSafe for Body
impl Send for Body
impl Sync for Body
impl Unpin for Body
impl UnwindSafe for Body
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more