Utilizing the Request Context and Data Bindings

As the saying goes, "context is everything" and within the Echo framework, everything is accessible through the request context. Context implementation within Echo allows for the simplification of otherwise difficult information passing, as well as state transference through middleware and handlers. Within Echo, we can think of the echo.Context type as a representation of the request itself and reference its methods in order to retrieve any information about the incoming request.

Along with context, converting the serialized representation of a request payload data into a structure that is usable within an application is of paramount concern for a web application. Echo provides a very helpful capability that will handle the deserialization of the request payload into a structure for use within your handlers. This allows the developer to create custom data structures within the web application, and by using the context.Bind method, convert the request into the developer-defined data structure.

Within web applications, where there is a request, there should be a response. This chapter will also cover the built-in capabilities of Echo's response rendering. These capabilities allow the developer to send the response HTTP code and a custom data structure representing the response payload as parameters. Echo then serializes this data structure into the wire format which is applicable for the response payload and writes the response to the http.ResponseWriter implementation.

By the end of this chapter, you will have learned about how Echo's context is a low cost abstraction that provides value to your application. These abstractions allow developers to focus on the business logic of the application instead of re-inventing the wheel for serialization and deserialization of data. Within this chapter, we will dive into the following topics, and show you how to implement these solutions within the Echo framework:

  • Maintaining the context through middleware and handlers
  • Extending the context interface with custom implementations
  • Request data binding capabilities
  • Response rendering capabilities

We will conclude this chapter with some example code that will show best practices for working with custom context implementations, as well as request data binding and response rendering using the Echo framework.

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

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