Skip to content

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.

auth := api.Http().Auth()

It returns an instance of IHttpCookie.

Middlewares

It returns an instance of IHttpMiddlewares that contains the built-in middlewares.

middlewares := api.Http().Middlewares()

Helpers

It returns an instance of the IHttpHelpers.

helpers := api.Http().Helpers()

HttpRouter

It returns an instance of IHttpRouterApi.

httpRouter := api.Http().HttpRouter()

HttpResponse

Returns an instance of IHttpResponse.

httpResponse := api.Http().HttpResponse()

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"
}

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

// handler
func (w http.ResponseWriter, r *http.Request) {
    formsAPI := api.Http().Forms()
    fmt.Println(formsAPI) // IHttpFormsApi
}