Reputation: 151
I'm trying to proxy a request from a Go backend to a microservice and modify the response before it is sent to the client. The request chain is: Client -> Go backend -> microservice -> Go backend -> client
I'm using the Go Gin framework. The working middleware:
func ReverseProxy(target string) gin.HandlerFunc {
log.Println(target)
url, err := url.Parse(target)
if err != nil {
log.Fatal(err)
}
proxy := httputil.NewSingleHostReverseProxy(url)
return func(c *gin.Context) {
proxy.ServeHTTP(c.Writer, c.Request)
}
}
Now my question is: How can I receive and modify the response sent by the microservice?
Upvotes: 1
Views: 4611
Reputation: 3559
How about using ReverseProxy.ModifyResponse?
For example, this will add a custom header to the response.
func addCustomHeader(r *http.Response) error {
r.Header["Hello"] = []string{"World"}
return nil
}
proxy.ModifyResponse = addCustomHeader
Upvotes: 4