Context
What is Context in Graphul?​
Context is a struct accessible in any handler and it provides the utils most used, with which we can access the params, queries and payload among other important properties.
- Using closure handler
- Using function handler
app.get("/", |c: Context| async move {
// ... your code
});
async fn my_handler(c: Context) {
// ... your code
}
note
If we declare a State into the routes we can use it from here.
Context methods​
Here is a table with the name, the return types and the description of every method
Method | Return type | Description |
---|---|---|
addr | SocketAddr | An internet socket address |
all_params | &HashMap<String, String> | Return all the path params with its values |
all_query | &HashMap<String, String> | Return all the query params with its values |
body | String | Return the body of the request into a String |
bytes | &Bytes | Return the body of the request in Bytes |
headers | &HeaderMap | Returns a reference to the associated header field map. |
ip | IpAddr | Return the Ip of the server |
json | Json<Value> | Return the body as a Json |
method | &Method | Return the method of the request |
params | String | Return all the values of the path param |
parse_params | Result<Json<T>, JsonRejection> | Return all path params into a Struct wrapped into a Json |
parse_query | Result<Json<T>, JsonRejection> | Return all queries params into a Struct wrapped into a Json |
payload | Result<Json<T>, JsonRejection> | Return the body as a Struct wrapped into a Json |
query | String | Return the value of a query param |
state | InnerState<T> | Return the state of the app, if you are not sharing a state the default value is a empty tuple «() » |
uri | &Uri | Returns a reference to the associated URI. |
version | Version | Returns the associated HTTP version. |