[go: up one dir, main page]

ureq

Struct BodyBuilder

source
pub struct BodyBuilder { /* private fields */ }
Expand description

Builder for creating a response body.

This is useful for testing, or for Middleware that returns another body than the requested one.

§Example

use ureq::Body;
use http::Response;

let body = Body::builder()
    .mime_type("text/plain")
    .charset("utf-8")
    .data("Hello world!");

let mut response = Response::builder()
    .status(200)
    .header("content-type", "text/plain; charset=utf-8")
    .body(body)?;

let text = response
    .body_mut()
    .read_to_string()?;

assert_eq!(text, "Hello world!");

Implementations§

source§

impl BodyBuilder

source

pub fn mime_type(self, mime_type: impl Into<String>) -> Self

Set the mime type of the body.

This does not set any HTTP headers. Affects Body decoding.

use ureq::Body;

let body = Body::builder()
    .mime_type("text/plain")
    .data("Hello world!");
source

pub fn charset(self, charset: impl Into<String>) -> Self

Set the mime type of the body

This does not set any HTTP headers. Affects Body decoding.

use ureq::Body;

let body = Body::builder()
    .mime_type("text/plain")
    .charset("utf-8")
    .data("Hello world!");
source

pub fn limit(self, l: u64) -> Self

Limit how much data is to be released from the body.

This does not set any HTTP headers. Affects Body decoding.

use ureq::Body;

let body = Body::builder()
    .mime_type("text/plain")
    .charset("utf-8")
    .limit(5)
    // This will be limited to "Hello"
    .data("Hello world!");
source

pub fn data(self, data: impl Into<Vec<u8>>) -> Body

Creates the body data turned into bytes.

Will automatically limit the body reader to the lenth of the data.

source

pub fn reader(self, data: impl Read + Send + Sync + 'static) -> Body

Creates a body from a streaming reader.

The reader can be limited by using .limit() or that the reader reaches the end.

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.