Enable GZIP middleware in the Echo Go web framework!


Compression is a critical aspect of web applications to improve performance and reduce bandwidth consumption. GZIP compression is a popular technique used for compressing data on the fly. Echo, a fast and flexible Go web framework, provides GZIP middleware to compress the HTTP response data automatically. In this article, we'll look at how to use the GZIP middleware in Echo to compress response data in a REST API.


To follow along with this tutorial, you should have the following:

  • Basic knowledge of Go programming language.

  • Echo Go web framework installed on your system.

  • A text editor or an IDE to write Go code.

  • A REST API server that serves data.


Before we start using the GZIP middleware, we need to add it to our Echo application. Here are the steps to do that:

  1. Import the GZIP middleware package:
import "github.com/labstack/echo/middleware"
  1. Add the middleware to the Echo instance:

Now, our Echo application is configured to use GZIP middleware.

REST API Go code:

To test the GZIP middleware, let's create a simple REST API endpoint that returns a large JSON response. Here's how to do that:

package main

import (


type User struct {
    Name     string `json:"name"`
    Email    string `json:"email"`
    Password string `json:"password"`

func main() {
    // create a new Echo instance
    e := echo.New()

    // add the GZIP middleware to Echo instance

    // define the REST API endpoint
    e.GET("/users", func(c echo.Context) error {
        // create a slice of users
        users := []User{
            {Name: "John Doe", Email: "john.doe@example.com", Password: "password"},
            {Name: "Jane Doe", Email: "jane.doe@example.com", Password: "password"},
            {Name: "Bob Smith", Email: "bob.smith@example.com", Password: "password"},

        // return the slice of users as JSON response
        return c.JSON(http.StatusOK, users)

    // start the server

In the above code, we've defined a REST API endpoint that returns a slice of User struct as JSON response.


To test the GZIP middleware, let's use the curl command-line tool to send a GET request to our REST API endpoint and see the response.

curl -H "Accept-Encoding: gzip" -i http://localhost:8000/users

The -H option sets the Accept-Encoding header to gzip to indicate that the client supports GZIP compression. The -i option shows the response headers along with the response body.

The response should be compressed with GZIP and have a Content-Encoding header with the value gzip.


In this article, we've seen how to use the GZIP middleware in the Echo Go web framework to compress the HTTP response data automatically. By adding the GZIP middleware, we can reduce the size of the response data, improve performance, and save bandwidth consumption.

I hope this helps, you!!

More such articles:





If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!