pub enum Body<'a> {
Empty,
String(Cow<'a, str>),
Json(Cow<'a, str>),
Bytes(Cow<'a, [u8]>),
}
Expand description
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
.
Implementations§
Source§impl Body<'_>
impl Body<'_>
Sourcepub fn default_content_type(&self) -> &str
pub fn default_content_type(&self) -> &str
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");
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
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");
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
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());
Sourcepub fn as_json<'b, T>(&'b self) -> Result<T>where
T: Deserialize<'b>,
pub fn as_json<'b, T>(&'b self) -> Result<T>where
T: Deserialize<'b>,
Deserializes the body as JSON to the requested type.
§Examples
use azure_functions::http::Body;
use std::borrow::Cow;
use serde::Deserialize;
#[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§
Source§impl<'a> Into<Body<'a>> for CosmosDbDocument
impl<'a> Into<Body<'a>> for CosmosDbDocument
Source§impl<'a> Into<Body<'a>> for EventHubMessage
impl<'a> Into<Body<'a>> for EventHubMessage
Source§impl<'a> Into<Body<'a>> for GenericInput
impl<'a> Into<Body<'a>> for GenericInput
Source§impl<'a> Into<Body<'a>> for OrchestrationData
impl<'a> Into<Body<'a>> for OrchestrationData
Source§impl<'a> Into<Body<'a>> for QueueMessage
impl<'a> Into<Body<'a>> for QueueMessage
Source§impl<'a> Into<Body<'a>> for ServiceBusMessage
impl<'a> Into<Body<'a>> for ServiceBusMessage
Source§impl<'a> Into<Body<'a>> for SignalRConnectionInfo
impl<'a> Into<Body<'a>> for SignalRConnectionInfo
Auto Trait Implementations§
impl<'a> Freeze for Body<'a>
impl<'a> RefUnwindSafe for Body<'a>
impl<'a> Send for Body<'a>
impl<'a> Sync for Body<'a>
impl<'a> Unpin for Body<'a>
impl<'a> UnwindSafe for Body<'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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request