kit_rs/http/
mod.rs

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