feather_runtime/http/
request.rs

1use crate::utils::error::Error;
2use bytes::Bytes;
3use http::{Extensions, HeaderMap, Method, Uri, Version};
4use std::fmt;
5
6
7#[derive(Debug, Clone)]
8pub struct HttpRequest {
9    /// The HTTP method of the request.<br>
10    /// For example, GET, POST, PUT, DELETE, etc.
11    pub method: Method,
12    /// The URI of the request.
13    pub uri: Uri,
14    /// The HTTP version of the request.
15    pub version: Version,
16    /// The headers of the request.
17    pub headers: HeaderMap,
18    /// The body of the request.
19    pub body: Bytes, // Changed from String to Bytes
20    /// The extensions of the request.
21    pub extensions: Extensions, // Added extensions field
22}
23
24impl HttpRequest {
25    /// Parses the body of the request as Serde JSON Value. Returns an error if the body is not valid JSON.
26    /// This method is useful for parsing JSON payloads in requests.
27    pub fn json(&self) -> Result<serde_json::Value, Error> {
28        serde_json::from_slice(&self.body).map_err(|e| {
29            Error::ParseError(format!("Failed to parse JSON body: {}", e))
30        })
31    }
32}
33
34impl fmt::Display for HttpRequest {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(
37            f,
38            "{} for {}: Body Data: {} ",
39            self.method,
40            self.uri,
41            String::from_utf8_lossy(&self.body)
42        ) 
43    }
44}