Trait actix_web::Responder [−][src]
pub trait Responder {
type Error: Into<Error>;
type Future: Future<Output = Result<Response, Self::Error>>;
fn respond_to(self, req: &HttpRequest) -> Self::Future;
fn with_status(self, status: StatusCode) -> CustomResponder<Self>
where
Self: Sized,
{ ... }
fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self>
where
Self: Sized,
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
{ ... }
}
Expand description
Trait implemented by types that can be converted to a http response.
Types that implement this trait can be used as the return type of a handler.
Associated Types
Required methods
fn respond_to(self, req: &HttpRequest) -> Self::Future
fn respond_to(self, req: &HttpRequest) -> Self::Future
Convert itself to AsyncResult
or Error
.
Provided methods
fn with_status(self, status: StatusCode) -> CustomResponder<Self> where
Self: Sized,
fn with_status(self, status: StatusCode) -> CustomResponder<Self> where
Self: Sized,
Override a status code for a Responder.
use actix_web::{HttpRequest, Responder, http::StatusCode};
fn index(req: HttpRequest) -> impl Responder {
"Welcome!".with_status(StatusCode::OK)
}
fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self> where
Self: Sized,
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self> where
Self: Sized,
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
Add header to the Responder’s response.
use actix_web::{web, HttpRequest, Responder};
use serde::Serialize;
#[derive(Serialize)]
struct MyObj {
name: String,
}
fn index(req: HttpRequest) -> impl Responder {
web::Json(
MyObj{name: "Name".to_string()}
)
.with_header("x-version", "1.2.3")
}