HttpApi
The HttpApi
is used to access various HTTP server functionalities including authentication, routing, and http responses.
1. HttpApi methods
The following are the available methods in HttpApi
:
GetClientDevice
Get the IClientDevice from the http request:
// http handler
func (w http.ResponseWriter, r *http.Request) {
clnt, err := api.Http().GetClientDevice(r)
fmt.Println(clnt) // IClientDevice
}
Auth
It returns an instance of the IHttpAuth.
Cookie
It returns an instance of IHttpCookie.
Middlewares
It returns an instance of IHttpMiddlewares that contains the built-in middlewares.
Helpers
It returns an instance of the IHttpHelpers.
HttpRouter
It returns an instance of IHttpRouterApi.
HttpResponse
Returns an instance of IHttpResponse.
MuxVars
Returns a map[string]string
of variables from the request path.
Below is an example to get the value if id
in the route path /sessions/:id
// handler
func (w http.ResponseWriter, r *http.Request) {
// other logic...
vars := api.Http().MuxVars(r) // map[string]string
id := vars["id"]
fmt.Println(id) // "1"
}
Navs
Returns an instance of INavsApi.
// handler
func (w http.ResponseWriter, r *http.Request) {
navsAPI := api.Http().Navs()
fmt.Println(navsAPI) // INavsApi
}
Forms
Returns an instance of IHttpFormsApi