macro_rules! post {
($post_handler: ident) => { ... };
}Expand description
Create a handler that accepts a post payload and returns a response.
You can accept raw bytes (Vec<u8>) as input, or any type for which Extract is implemented.
If you choose to use an extracted type, this will automatically return a 400 error containing
the error details if the input bytes cannot be extracted into the specified input type.
If you would rather handle extraction errors yourself, you should accept raw bytes as input
and perform extraction yourself.
Your implementation function must return a value which implements the IntoWebResponse trait. Implementations of this trait are provided for
- [WebResponse]: A basic response representation and builder
WebResult<impl IntoWebResponse>: Allows you to return results where errors will be converted to 500 responses.- [()]: Results in an empty 204.
- String and &str: Results in a 200 with the string body.
Vec<u8>and&[u8]: Results in a 200 with the binary body.- [Json]: Results in a 200 with the Json body, or a 500 if the Json could not be serialized.
You may also implement IntoWebResponse for your own types.
Raw Bytes Input:
use std::error::Error;///
use momento_functions::WebResponse;
momento_functions::post!(ping);
fn ping(payload: Vec<u8>) -> &'static str {
"pong"
}Typed JSON Input:
use std::error::Error;
use momento_functions::WebResponse;
use momento_functions_host::encoding::Json;
#[derive(serde::Deserialize)]
struct Request {
name: String,
}
#[derive(serde::Serialize)]
struct Response {
message: String,
}
momento_functions::post!(greet);
fn greet(Json(request): Json<Request>) -> Json<Response> {
Json(Response { message: format!("Hello, {}!", request.name) })
}