[−][src]Struct obsidian::context::Context
Context contains the data for current http connection context. For example, request information, params, method, and path.
Methods
impl Context
[src]
pub fn new(request: Request<Body>, params_data: HashMap<String, String>) -> Self
[src]
pub fn headers(&self) -> &HeaderMap<HeaderValue>
[src]
Access request header
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
[src]
Access mutable request header
pub fn method(&self) -> &Method
[src]
Access request method
pub fn uri(&self) -> &Uri
[src]
Access request uri
pub fn param<T: FromStr>(&self, key: &str) -> Result<T, ObsidianError>
[src]
Method to get the params value according to key. Panic if key is not found.
Example
// Assumming ctx contains params for id and mode fn get_handler(ctx: Context) -> impl Responder { let id: i32 = ctx.param("id").unwrap(); let mode: String = ctx.param("mode").unwrap(); assert_eq!(id, 1); assert_eq!(mode, "edit".to_string()); StatusCode::OK }
pub fn uri_query<T: DeserializeOwned>(&mut self) -> Result<T, ObsidianError>
[src]
Method to get the string query data from the request url. Untagged is not supported
Example
#[derive(Deserialize, Serialize, Debug)] struct QueryString { id: i32, mode: String, } // Assume ctx contains string query with data {id=1&mode=edit} fn get_handler(mut ctx: Context) -> impl Responder { let result: QueryString = ctx.uri_query().unwrap(); assert_eq!(result.id, 1); assert_eq!(result.mode, "edit".to_string()); StatusCode::OK }
pub fn form<T: DeserializeOwned>(&mut self) -> Result<T, ObsidianError>
[src]
Method to get the forms query data from the request body. Body is consumed after calling this method. Untagged is not supported
Example
#[derive(Deserialize, Serialize, Debug)] struct FormResult { id: i32, mode: String, } // Assume ctx contains form query with data {id=1&mode=edit} fn get_handler(mut ctx: Context) -> impl Responder { let result: FormResult = ctx.form().unwrap(); assert_eq!(result.id, 1); assert_eq!(result.mode, "edit".to_string()); StatusCode::OK }
pub fn form_with_param<T: DeserializeOwned>(&mut self) -> Result<T, ()>
[src]
Form value merge with Params
pub fn json<T: DeserializeOwned>(&mut self) -> Result<T, ObsidianError>
[src]
Method to get the json data from the request body. Body is consumed after calling this method. The result can be either handled by using static type or dynamic map. Panic if parsing fail.
Example
Handle by static type
#[derive(Deserialize, Serialize, Debug)] struct JsonResult { id: i32, mode: String, } // Assume ctx contains json with data {id:1, mode:'edit'} fn get_handler(mut ctx: Context) -> impl Responder { let result: JsonResult = ctx.json().unwrap(); assert_eq!(result.id, 1); assert_eq!(result.mode, "edit".to_string()); StatusCode::OK }
Handle by dynamic map
// Assume ctx contains json with data {id:1, mode:'edit'} fn get_handler(mut ctx: Context) -> impl Responder { let result: serde_json::Value = ctx.json().unwrap(); assert_eq!(result["id"], 1); assert_eq!(result["mode"], "edit".to_string()); StatusCode::OK }
pub fn json_with_param<T: DeserializeOwned>(
&mut self
) -> Result<T, ObsidianError>
[src]
&mut self
) -> Result<T, ObsidianError>
Json value merged with Params
pub fn take_body(&mut self) -> Body
[src]
Consumes body of the request and replace it with empty body.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Context
impl Send for Context
impl !Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,