Echo context

The echo.Context interface defines the following methods which provide the developer with helper functions to retrieve various information about the request that is made, as well as manipulate the state of the context. The following is a shortened list of important methods available on the context, along with a short description of what each method provides:

  • Param(name string) string:
    • Returns the value of the URL parameter that is mapped to by name
    • This is the primary interface by which you retrieve URL parameters within your handler and middleware functions
    • The name parameter provided to this function is the string that was used within the routing path definition
  • QueryParam(name string) string:
    • Returns the value of the URL query parameter that is mapped to by name
    • This allows you to access the query parameter by parameter name instead of using the standard library's http.Request URL to access the Query method
    • Designed as a helper function to access query string parameters
  • FormValue(name string) string:
    • Returns the value of the form payload parameter that is mapped to by name
    • This will return the value of a submitted form's attribute defined by name
  • FormFile(name string) (*multipart.FileHeader, error):
    • Returns the multi-part form file that is mapped to by name
  • Cookie(name string) (*http.Cookie, error):
    • Returns the cookie that is mapped to by name from the request
  • SetCookie(cookie *http.Cookie):
    • Adds a Set-Cookie response header, which is useful for setting cookie values
  • Get(key string) interface{}:
    • Retrieves an arbitrary value from the context that is mapped to by key
    • This is useful for retrieving the state that was set by middleware and other handler functions
  • Set(key string, val interface{}):
    • Sets an arbitrary key/value pair on the context
    • This is useful for setting the state for use in future middleware or handlers
  • Handler() HandlerFunc:
    • Returns the handler function found in the router
    • This is the actual function that the Echo framework is going to perform based on the routing results
  • Logger() Logger:
    • This will return an instance of the Echo logger
    • With this, you will be able to use the built-in Echo logging infrastructure to create logs

The remaining functionality of the echo.Context primarily revolves around input binding and validation of request payloads, as well as rendering capabilities provided by Echo. These topics will be defined in-depth in the following sections of this chapter. It is important to remember when developing an application with the Echo framework that context is a central primitive of the framework, which will increase your efficiency in development.

As you can see, the context is truly everything within the Echo framework. The contextual information provided to the middleware and handlers allows for state to be created and passed along the request processing pipeline through middleware and handler functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223