Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Echo Quick Start Guide

You're reading from  Echo Quick Start Guide

Product type Book
Published in May 2018
Publisher Packt
ISBN-13 9781789139433
Pages 136 pages
Edition 1st Edition
Languages
Author (1):
Ben Huson Ben Huson
Profile icon Ben Huson

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...

Technical requirements

Maintaining context

Throughout early Go web application development, there was much contention surrounding how to weave together context when chaining handler function calls together. If we examine the standard library http.HandlerFunc definition, type HandlerFunc func(ResponseWriter, *Request), this type is not conducive for building a chained middleware scheme, or calling other request handlers from within handlers whatsoever. This is because of the two different parameters, http.ResponseWriter and *http.Request. The http.ResponseWriter is an interface that is designed primarily to write a sequence of bytes back to the caller, whereas the second parameter, the *http.Request, is a pointer to the data structure that represents the request.

Prior to Go 1.7, there was no mechanism in place within the http.Request type to store extra information to keep the state from one nested...

Request binding

Within the context interface in the Echo framework, there is a method called Bind, which will perform request payload binding. The following is the definition of the function:

    // Bind binds the request body into provided type `i`. The default binder
    // does it based on Content-Type header.
    Bind(i interface{}) error

In essence, the Context.Bind function takes the payload and Content-Type header from the HTTP request, and converts it into any structure defined by the passed in i interface{} function parameter. This is a very handy feature of the web application framework as it removes a lot of the coding required by the developer to interpret the request payload themselves. Instead of having to perform deserialization of the HTTP request body yourself, Echo will perform this for you. The following is an example handler that uses the Bind function call...

Response rendering

As mentioned in Chapter 1, Understanding HTTP, Go, and Echo, the primary purpose of a web application is to take in a request, and render a response to the caller. Fortunately for us, Echo has a multitude of ways we can render responses back to callers. The Echo Context has the following helpers that can be used to render responses, so that you as a developer will not need to serialize the response data yourself:

HTML(code int, html string) error
HTMLBlob(code int, b []byte) error
String(code int, s string) error
JSON(code int, i interface{}) error
JSONPretty(code int, i interface{}, indent string) error
JSONBlob(code int, b []byte) error
JSONP(code int, callback string, i interface{}) error
JSONPBlob(code int, callback string, b []byte) error
XML(code int, i interface{}) error
XMLPretty(code int, i interface{}, indent string) error
XMLBlob(code int, b []byte...

Summary

Within this chapter, we have covered quite a few important aspects of the Echo framework. These aspects include context, request binding, and response rendering. We have compared and contrasted the difficulties in designing a web framework that will allow us to abstract business logic into middleware by passing the context from one handler function to the next in the processing chain. We also discovered the key features of the Echo Context, as well as the useful features for binding and validating of data, as well as rendering responses.

The next chapter will be dedicated to logging and error handling capabilities within the Echo framework. We will cover how best to organize your code to take advantage of logging. We will also explain how errors are handled within Echo.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Echo Quick Start Guide
Published in: May 2018 Publisher: Packt ISBN-13: 9781789139433
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}