Struct poem_openapi::payload::Binary [−][src]
pub struct Binary<T>(pub T);
Expand description
A binary payload.
Examples
use poem::{
error::BadRequest,
http::{Method, StatusCode, Uri},
IntoEndpoint, Request, Result,
};
use poem_openapi::{
payload::{Binary, BinaryStream, Json},
OpenApi, OpenApiService,
};
use tokio::io::AsyncReadExt;
struct MyApi;
#[OpenApi]
impl MyApi {
#[oai(path = "/upload", method = "post")]
async fn upload_binary(&self, data: Binary<Vec<u8>>) -> Json<usize> {
Json(data.len())
}
#[oai(path = "/upload_stream", method = "post")]
async fn upload_binary_stream(&self, data: Binary<BinaryStream>) -> Result<Json<usize>> {
let mut reader = data.0.into_async_read();
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await.map_err(BadRequest)?;
Ok(Json(bytes.len()))
}
}
let api = OpenApiService::new(MyApi, "Demo", "0.1.0").into_endpoint();
let resp = api
.call(
Request::builder()
.method(Method::POST)
.content_type("application/octet-stream")
.uri(Uri::from_static("/upload"))
.body("abcdef"),
)
.await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "6");
let resp = api
.call(
Request::builder()
.method(Method::POST)
.content_type("application/octet-stream")
.uri(Uri::from_static("/upload_stream"))
.body("abcdef"),
)
.await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "6");
Tuple Fields
0: T
Trait Implementations
Register the schema contained in this response object to the registry.
If true, it means that the response object has a custom bad request handler. Read more
Convert ParseRequestError
to this response object.
Consume itself and return Response
.
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self> where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self> where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Wrap an impl IntoResponse
to add a header. Read more
Wrap an impl IntoResponse
to set a status code. Read more
fn from_request<'life0, 'life1, 'async_trait>(
request: &'life0 Request,
body: &'life1 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self, ParseRequestError>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'life1, 'async_trait>(
request: &'life0 Request,
body: &'life1 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self, ParseRequestError>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Parse the payload object from the HTTP request.
fn from_request<'life0, 'life1, 'async_trait>(
request: &'life0 Request,
body: &'life1 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self, ParseRequestError>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'life1, 'async_trait>(
request: &'life0 Request,
body: &'life1 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self, ParseRequestError>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Parse the payload object from the HTTP request.
fn from_request<'life0, 'life1, 'async_trait>(
request: &'life0 Request,
body: &'life1 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self, ParseRequestError>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'life1, 'async_trait>(
request: &'life0 Request,
body: &'life1 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self, ParseRequestError>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Parse the payload object from the HTTP request.
The content type of this payload.
Gets schema reference of this payload.
If it is true
, it means that this payload is required.
Auto Trait Implementations
impl<T> RefUnwindSafe for Binary<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Binary<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more