Go middleware example. How to alter a HTTP handler result?
Tags: go, middleware, handler, webdev
Published: 2019-10-24 13:39:29

---
title: Go middleware example. How to alter a HTTP handler result?
published: true
description: Let's imagine a situation when you want to alter a result, returned by some http handler to the client. Fortunately, Golang provides an easy mechanism for that, called a middleware.
tags: golang, go, middleware, handler, webdev
---

Let's imagine a situation when you want to alter a result, returned by some http handler to the client. Fortunately, Golang provides an easy mechanism for that, called a middleware.

I will dive directly into the source code, to save your time.

Imagine, we have this simple webserver (here I'm using a chi router):

```go
package main

import (
   "bytes""github.com/go-chi/chi""io""log""net/http"
)

func main() {
   r := chi.NewRouter()
   r.Get("/", myFirstHandler)

   http.ListenAndServe(":3000", r)
}

func myFirstHandler(w http.ResponseWriter, r *http.Request) {
   w.Write([]byte("This is a main page"))
}

```

When we run this application and visit a frontpage htttp://localhost:3000/, you can see this:
![golang golang](https://thepracticaldev.s3.amazonaws.com/i/utibvawycaueheyjoqap.png)

And now we got a new requirement to create another handler which should get all response data from myFirstHandler and add some modification on top.

We can do it easily in this way:
```go
// Adds a new router handler with a middleware myMiddleware.  
r.With(myMiddleware).Get("/other", myFirstHandler) 

```
To be able to read a response from another handler, we have to implement our own ResponseWriter:

```go
type MyResponseWriter struct {
   http.ResponseWriter
   buf *bytes.Buffer
}

// Here we are implementing a Write() function from ResponseWriter with our custom instructions. 
func (myrw *MyResponseWriter) Write(p []byte) (int, error) {
   return myrw.buf.Write(p)
}
```
And finally, let's write our middleware:
```go
func myMiddleware(next http.Handler) http.Handler {
   return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      // Create a response writer:
      myResponseWriter := &MyResponseWriter{
         ResponseWriter: w,
         buf:            &bytes.Buffer{},
      }
      // Here we are pssing our custom response writer to the next http handler.
      next.ServeHTTP(myResponseWriter, r)
      
      // Here we are adding our custom stuff to the response, which we received after http handler execution. 
      myResponseWriter.buf.WriteString(" and some additional modifications")
      
      // And, finally, we are copiing everything back to the original response writer.  if _, err := io.Copy(w, myResponseWriter.buf); err != nil {
         log.Printf("Failed to send out response: %v", err)
      }
   })
}
```
Now, if we run our server again and go to /other path, we will see this:
![golang golang](https://thepracticaldev.s3.amazonaws.com/i/kcvyfo1f2q89e7et0dh6.png)

This was a silly example, which will never happen in real life, but, I hope you got an overview of how you can play with HTTP handlers and middlewares.



The source code you can find in this repository: https://github.com/alexsergivan/blog-examples/tree/master/middleware