Attribute Macro spin_sdk::http_component
source · #[http_component]Expand description
Exports the procedural macros for writing handlers for Spin components. The entrypoint to a WASI HTTP component written in Rust.
Functions annotated with this attribute can be of two forms:
- Request/Response
- Input/Output Params
When in doubt prefer the Request/Response variant unless streaming response bodies is something you need.
§Request/Response
This form takes the form of a function with one request param and one response return value.
Requests are anything that implements spin_sdk::http::conversions::TryFromIncomingRequest which includes
spin_sdk::http::Request, spin_sdk::http::IncomingRequest, and even hyperium’s popular http crate’s Request
type.
Responses are anything that implements spin_sdk::http::IntoResponse. This includes Result<impl IntoResponse, impl IntoResponse,
spin_sdk::http::Response, and even the http crate’s Response type.
For example:
use spin_sdk::http_component;
use spin_sdk::http::{Request, IntoResponse};
#[http_component]
async fn my_handler(request: Request) -> anyhow::Result<impl IntoResponse> {
// Your logic goes here
}§Input/Output Params
Input/Output functions allow for streaming HTTP bodies. This form is by its very nature harder to use than the request/response form above so it should only be favored when stream response bodies is desired.
The request param can be anything that implements spin_sdk::http::TryFromIncomingRequest. And
the response_out param must be a spin_sdk::http::ResponseOutparam. See the docs of ResponseOutparam
for how to use this type.
For example:
use spin_sdk::http_component;
use spin_sdk::http::{IncomingRequest, ResponseOutparam};
#[http_component]
async fn my_handler(request: IncomingRequest, response_out: ResponseOutparam) {
// Your logic goes here
}