pub struct Response {
pub status: Status,
pub headers: ResponseHeaders,
/* private fields */
}Expand description
§HTTP Response
Composed of
statusheaderscontent
§Usage
in_fang.rs
use ohkami::prelude::*;
#[derive(Clone)]
struct SetHeaders;
impl FangAction for SetHeaders {
async fn back<'a>(&'a self, res: &'a mut Response) {
res.headers.set()
.server("ohkami")
.vary("Origin");
}
}
#[tokio::main]
async fn main() {
Ohkami::new((SetHeaders,
"/".GET(|| async {"Hello, ohkami!"})
)).howl("localhost:5050").await
}into_response.rs
use ohkami::{Response, IntoResponse, Status};
enum AppError {
A(String),
B(String),
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
match self {
Self::A(msg) => Response::InternalServerError().with_text(msg),
Self::B(msg) => Response::BadRequest().with_text(msg),
}
}
}
async fn handler(id: usize) -> Result<String, AppError> {
if id == 0 {
return Err(AppError::B("id must be positive".into()))
}
Ok("Hello, Response!".into())
}Fields§
§status: StatusHTTP status of this response
headers: ResponseHeadersHeaders of this response
.{name}(),.get("{name}")to get value.set().{name}({action}),.set().x("{name}", {action})to mutate values
{action}:
- just
{value}to insert Noneto removeheader::append({value})to append
{value}:
String&'static strCow<'static, str>Some(Cow<'static, str>)
Implementations§
Source§impl Response
impl Response
Sourcepub fn SwitchingProtocols() -> Self
pub fn SwitchingProtocols() -> Self
101 Switching Protocols
empty Response
Sourcepub fn Processing() -> Self
pub fn Processing() -> Self
102 Processing
empty Response
Sourcepub fn EarlyHints() -> Self
pub fn EarlyHints() -> Self
103 Early Hints
empty Response
Sourcepub fn NonAuthoritativeInformation() -> Self
pub fn NonAuthoritativeInformation() -> Self
203 Non-Authoritative Information
empty Response
Sourcepub fn ResetContent() -> Self
pub fn ResetContent() -> Self
205 Reset Content
empty Response
Sourcepub fn PartialContent() -> Self
pub fn PartialContent() -> Self
206 Partial Content
empty Response
Sourcepub fn MultiStatus() -> Self
pub fn MultiStatus() -> Self
207 Multi-Status
empty Response
Sourcepub fn AlreadyReported() -> Self
pub fn AlreadyReported() -> Self
208 Already Reported
empty Response
Sourcepub fn MultipleChoice() -> Self
pub fn MultipleChoice() -> Self
300 Multiple Choice
empty Response
Sourcepub fn MovedPermanently() -> Self
pub fn MovedPermanently() -> Self
301 Moved Permanently
empty Response
Sourcepub fn NotModified() -> Self
pub fn NotModified() -> Self
304 Not Modifed
empty Response
Sourcepub fn TemporaryRedirect() -> Self
pub fn TemporaryRedirect() -> Self
307 Temporary Redirect
empty Response
Sourcepub fn PermanentRedirect() -> Self
pub fn PermanentRedirect() -> Self
308 Permanent Redirect
empty Response
Sourcepub fn BadRequest() -> Self
pub fn BadRequest() -> Self
400 Bad Request
empty Response
401 Unauthorized
empty Response
Sourcepub fn MethodNotAllowed() -> Self
pub fn MethodNotAllowed() -> Self
405 Method Not Allowed
empty Response
Sourcepub fn NotAcceptable() -> Self
pub fn NotAcceptable() -> Self
406 Not Acceptable
empty Response
Sourcepub fn ProxyAuthenticationRequired() -> Self
pub fn ProxyAuthenticationRequired() -> Self
407 Proxy Authentication Required
empty Response
Sourcepub fn RequestTimeout() -> Self
pub fn RequestTimeout() -> Self
408 Request Timeout
empty Response
Sourcepub fn LengthRequired() -> Self
pub fn LengthRequired() -> Self
411 Length Required
empty Response
Sourcepub fn PreconditionFailed() -> Self
pub fn PreconditionFailed() -> Self
412 Precondition Failed
empty Response
Sourcepub fn PayloadTooLarge() -> Self
pub fn PayloadTooLarge() -> Self
413 Payload Too Large
empty Response
Sourcepub fn URITooLong() -> Self
pub fn URITooLong() -> Self
414 URI Too Long
empty Response
Sourcepub fn UnsupportedMediaType() -> Self
pub fn UnsupportedMediaType() -> Self
415 Unsupported Media Type
empty Response
Sourcepub fn RangeNotSatisfiable() -> Self
pub fn RangeNotSatisfiable() -> Self
416 Range Not Satisfiable
empty Response
Sourcepub fn ExceptionFailed() -> Self
pub fn ExceptionFailed() -> Self
417 Exception Failed
empty Response
Sourcepub fn MisdirectedRequest() -> Self
pub fn MisdirectedRequest() -> Self
421 Misdirected Request
empty Response
Sourcepub fn UnprocessableEntity() -> Self
pub fn UnprocessableEntity() -> Self
422 Unprocessable Entity
empty Response
Sourcepub fn FailedDependency() -> Self
pub fn FailedDependency() -> Self
424 Failed Dependency
empty Response
Sourcepub fn UpgradeRequired() -> Self
pub fn UpgradeRequired() -> Self
426 UpgradeRequired
empty Response
Sourcepub fn PreconditionRequired() -> Self
pub fn PreconditionRequired() -> Self
428 Precondition Required
empty Response
Sourcepub fn TooManyRequest() -> Self
pub fn TooManyRequest() -> Self
429 Too Many Request
empty Response
Sourcepub fn RequestHeaderFieldsTooLarge() -> Self
pub fn RequestHeaderFieldsTooLarge() -> Self
431 Request Header Fields Too Large
empty Response
451 Unavailable For Legal Reasons
empty Response
Sourcepub fn InternalServerError() -> Self
pub fn InternalServerError() -> Self
500 Internal Server Error
empty Response
Sourcepub fn NotImplemented() -> Self
pub fn NotImplemented() -> Self
501 Not Implemented
empty Response
Sourcepub fn BadGateway() -> Self
pub fn BadGateway() -> Self
502 Bad Gateway
empty Response
503 Service Unavailable
empty Response
Sourcepub fn GatewayTimeout() -> Self
pub fn GatewayTimeout() -> Self
504 Gateway Timeout
empty Response
Sourcepub fn HTTPVersionNotSupported() -> Self
pub fn HTTPVersionNotSupported() -> Self
505 HTTP Version Not Supported
empty Response
Sourcepub fn VariantAlsoNegotiates() -> Self
pub fn VariantAlsoNegotiates() -> Self
506 Variant Also Negotiates
empty Response
Sourcepub fn InsufficientStorage() -> Self
pub fn InsufficientStorage() -> Self
507 Unsufficient Storage
empty Response
Sourcepub fn LoopDetected() -> Self
pub fn LoopDetected() -> Self
508 Loop Detected
empty Response
Sourcepub fn NotExtended() -> Self
pub fn NotExtended() -> Self
510 Not Extended
empty Response
Sourcepub fn NetworkAuthenticationRequired() -> Self
pub fn NetworkAuthenticationRequired() -> Self
511 Network Authentication Required
empty Response
Source§impl Response
impl Response
pub fn with_headers( self, f: impl FnOnce(SetHeaders<'_>) -> SetHeaders<'_>, ) -> Self
pub fn drop_content(&mut self) -> Content
pub fn without_content(self) -> Self
pub fn set_payload( &mut self, content_type: &'static str, content: impl Into<Cow<'static, [u8]>>, )
pub fn with_payload( self, content_type: &'static str, content: impl Into<Cow<'static, [u8]>>, ) -> Self
pub fn payload(&self) -> Option<&[u8]>
pub fn set_text<Text: Into<Cow<'static, str>>>(&mut self, text: Text)
pub fn with_text<Text: Into<Cow<'static, str>>>(self, text: Text) -> Self
pub fn set_html<HTML: Into<Cow<'static, str>>>(&mut self, html: HTML)
pub fn with_html<HTML: Into<Cow<'static, str>>>(self, html: HTML) -> Self
pub fn set_json<JSON: Serialize>(&mut self, json: JSON)
pub fn with_json<JSON: Serialize>(self, json: JSON) -> Self
Source§impl Response
impl Response
pub fn with_stream<T: Data>( self, stream: impl Stream<Item = T> + Unpin + Send + 'static, ) -> Self
sse only.pub fn set_stream<T: Data>( &mut self, stream: impl Stream<Item = T> + Unpin + Send + 'static, )
sse only.pub fn set_stream_raw( &mut self, stream: Pin<Box<dyn Stream<Item = String> + Send>>, )
sse only.Trait Implementations§
Source§impl FromResidual<Result<Infallible, Response>> for Response
Available on crate feature nightly only.
impl FromResidual<Result<Infallible, Response>> for Response
nightly only.Source§fn from_residual(residual: Result<Infallible, Response>) -> Self
fn from_residual(residual: Result<Infallible, Response>) -> Self
try_trait_v2)Residual type. Read more