pub struct SendBody<'a> { /* private fields */ }Expand description
Request body for sending data via POST, PUT and PATCH.
Typically not interacted with directly since the trait AsSendBody is implemented
for the majority of the types of data a user might want to send to a remote server.
That means if you want to send things like String, &str or [u8], they can be
used directly. See documentation for AsSendBody.
The exception is when using Read trait bodies, in which case we wrap the request
body directly. See below SendBody::from_reader.
Implementations§
Source§impl<'a> SendBody<'a>
impl<'a> SendBody<'a>
Sourcepub fn from_reader(reader: &'a mut dyn Read) -> SendBody<'a>
pub fn from_reader(reader: &'a mut dyn Read) -> SendBody<'a>
Creates a body from a shared Read impl.
Sourcepub fn from_owned_reader(reader: impl Read + 'static) -> SendBody<'static>
pub fn from_owned_reader(reader: impl Read + 'static) -> SendBody<'static>
Creates a body from an owned [Read] impl.
Sourcepub fn from_json(value: &impl Serialize) -> Result<SendBody<'static>, Error>
pub fn from_json(value: &impl Serialize) -> Result<SendBody<'static>, Error>
Creates a body to send as JSON from any Serialize value.
Sourcepub fn into_reader(self) -> impl Sized + Read + 'a
pub fn into_reader(self) -> impl Sized + Read + 'a
Turn this SendBody into a reader.
This is useful in Middleware to make changes to the
body before sending it.
use ureq::{SendBody, Body};
use ureq::middleware::MiddlewareNext;
use ureq::http::{Request, Response, header::HeaderValue};
use std::io::Read;
fn my_middleware(req: Request<SendBody>, next: MiddlewareNext)
-> Result<Response<Body>, ureq::Error> {
// Take apart the request.
let (parts, body) = req.into_parts();
// Take the first 100 bytes of the incoming send body.
let mut reader = body.into_reader().take(100);
// Create a new SendBody.
let new_body = SendBody::from_reader(&mut reader);
// Reconstitute the request.
let req = Request::from_parts(parts, new_body);
// set my bespoke header and continue the chain
next.handle(req)
}Trait Implementations§
impl<'a> AsSendBody for SendBody<'a>
Auto Trait Implementations§
impl<'a> !Freeze for SendBody<'a>
impl<'a> !RefUnwindSafe for SendBody<'a>
impl<'a> !Send for SendBody<'a>
impl<'a> !Sync for SendBody<'a>
impl<'a> Unpin for SendBody<'a>
impl<'a> !UnwindSafe for SendBody<'a>
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