pub trait Responder {
// Required method
fn to_response(self, req: HttpRequest) -> HttpResponse;
}Expand description
A worker’s custom response implementation.
§Examples
use serde::{Deserialize, Serialize};
use worker::{Request, Response, RouteContext};
use worker_route::{get, HttpResponse, HttpRequest, http::ResponseBuilder, Responder};
#[allow(unused)]
#[derive(Deserialize, Serialize)]
struct Foo {
foo: String,
}
impl Responder for Foo {
fn to_response(self, _req: HttpRequest) -> HttpResponse {
ResponseBuilder::init().json(self)
}
}
#[get("/custom_response")]
async fn custom_response(_: Request, _: RouteContext<()>) -> Result<Foo, worker::Error> {
Ok(Foo { foo: String::from("Bar") })
}Required Methods§
Sourcefn to_response(self, req: HttpRequest) -> HttpResponse
fn to_response(self, req: HttpRequest) -> HttpResponse
Convert Self to HttpResponse