kit_rs/http/
mod.rs

1mod body;
2mod extract;
3mod form_request;
4mod request;
5mod response;
6
7pub use body::{collect_body, parse_form, parse_json};
8pub use extract::{FromParam, FromRequest};
9pub use form_request::FormRequest;
10pub use request::{Request, RequestParts};
11pub use response::{HttpResponse, Redirect, RedirectRouteBuilder, Response, ResponseExt};
12
13/// Error type for missing route parameters
14///
15/// This type is kept for backward compatibility. New code should use
16/// `FrameworkError::param()` instead.
17#[derive(Debug)]
18pub struct ParamError {
19    pub param_name: String,
20}
21
22impl From<ParamError> for HttpResponse {
23    fn from(err: ParamError) -> HttpResponse {
24        HttpResponse::json(serde_json::json!({
25            "error": format!("Missing required parameter: {}", err.param_name)
26        }))
27        .status(400)
28    }
29}
30
31impl From<ParamError> for crate::error::FrameworkError {
32    fn from(err: ParamError) -> crate::error::FrameworkError {
33        crate::error::FrameworkError::ParamError {
34            param_name: err.param_name,
35        }
36    }
37}
38
39impl From<ParamError> for Response {
40    fn from(err: ParamError) -> Response {
41        Err(HttpResponse::from(crate::error::FrameworkError::from(err)))
42    }
43}
44
45/// Create a text response
46pub fn text(body: impl Into<String>) -> Response {
47    Ok(HttpResponse::text(body))
48}
49
50/// Create a JSON response from a serde_json::Value
51pub fn json(body: serde_json::Value) -> Response {
52    Ok(HttpResponse::json(body))
53}