Need help in rewriting the response #1992
Unanswered
palashjain95
asked this question in
Q&A
Replies: 1 comment
-
Are you trying to change external service (out of this go process) response inside Echo or you want to change some (wrapped) handler response before it is served to the client? If you want to replace/modify proxied request response body then this will help you https://stackoverflow.com/questions/31535569/how-to-read-response-body-of-reverseproxy Proxy middleware supports providing your own Lines 57 to 58 in c6f0c66 Middleware definition would be something like that if we combine StackOverflow example: url1, _ := url.Parse("http://target:80")
e.Use(ProxyWithConfig(ProxyConfig{
Balancer: NewRoundRobinBalancer([]*ProxyTarget{
{
Name: "target 1",
URL: url1,
},
}),
ModifyResponse: func(resp *http.Response) error {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
b = bytes.Replace(b, []byte("server"), []byte("schmerver"), -1)
body := ioutil.NopCloser(bytes.NewReader(b))
resp.Body = body
resp.ContentLength = int64(len(b))
resp.Header.Set("Content-Length", strconv.Itoa(len(b)))
return nil
},
})) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to hijack and rewrite the response after the api call has been serviced by the business logic.
Kind like making use of modify response function in body dump middleware. Struggling on it for some time. Can some please help with this.
Original Response
Expected Response
Actual Response
Tried with proxy middleware
In this I am getting gateway timeout with multiple calls to my server. Can't understand how the proxy middleware works
Beta Was this translation helpful? Give feedback.
All reactions