[][src]Enum azure_functions::http::Body

pub enum Body<'a> {
    Empty,
    String(Cow<'a, str>),
    Json(Cow<'a, str>),
    Bytes(Cow<'a, [u8]>),
}

Represents the body of a HTTP request or response.

Variants

Empty

Represents an empty body.

String(Cow<'a, str>)

Represents a string body with a default content type of text/plain.

Json(Cow<'a, str>)

Represents a JSON body with a default content type of application/json.

Bytes(Cow<'a, [u8]>)

Represents a body from a slice of bytes with a default content type of application/octet-stream.

Methods

impl<'_> Body<'_>[src]

pub fn default_content_type(&self) -> &str[src]

Gets the default content type for a body.

Returns application/json for Body::Json.

Returns application/octet-stream for Body::Bytes.

Returns text/plain for all other Body values.

Examples

use azure_functions::http::Body;

let body: Body = [1, 2, 3][..].into();

assert_eq!(body.default_content_type(), "application/octet-stream");

pub fn as_str(&self) -> Option<&str>[src]

Gets the body as a string.

Returns None if there is no valid string representation of the message.

Examples

use azure_functions::http::Body;
use std::borrow::Cow;

let body = Body::String(Cow::Borrowed("test"));
assert_eq!(body.as_str().unwrap(), "test");

pub fn as_bytes(&self) -> &[u8][src]

Gets the body as a slice of bytes.

Examples

use azure_functions::http::Body;
use std::borrow::Cow;

let body = Body::String(Cow::Borrowed("test"));
assert_eq!(body.as_bytes(), "test".as_bytes());

pub fn as_json<'b, T>(&'b self) -> Result<T> where
    T: Deserialize<'b>, 
[src]

Deserializes the body as JSON to the requested type.

Examples

use azure_functions::http::Body;
use std::borrow::Cow;

#[derive(Deserialize)]
struct Data {
    message: String
}

let body = Body::String(Cow::Borrowed(r#"{ "message": "hello" }"#));
let data = body.as_json::<Data>().unwrap();
assert_eq!(data.message, "hello");

Trait Implementations

impl<'a> Clone for Body<'a>[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<'_> Into<TypedData> for Body<'_>[src]

impl<'a> From<&'a TypedData> for Body<'a>[src]

impl<'a> From<&'a str> for Body<'a>[src]

impl<'_> From<String> for Body<'_>[src]

impl<'_, '_> From<&'_ Value> for Body<'_>[src]

impl<'_> From<Value> for Body<'_>[src]

impl<'a> From<&'a [u8]> for Body<'a>[src]

impl<'_> From<Vec<u8>> for Body<'_>[src]

impl<'a> Debug for Body<'a>[src]

impl<'_> Display for Body<'_>[src]

Auto Trait Implementations

impl<'a> Send for Body<'a>

impl<'a> Sync for Body<'a>

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Erased for T