0

Labstack Echo v4 has recently been updated to include ModifyResponse hook (ala golang's httputil ReverseProxy).

Most of the examples of using this seem to leverage ioutil.ReadAll().

For example:

func UpdateResponse(r *http.Response) error {
    b, _ := ioutil.ReadAll(r.Body)
    buf := bytes.NewBufferString("Monkey")
    buf.Write(b)
    r.Body = ioutil.NopCloser(buf)
    r.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())}
    return nil
}

What I am looking to do is to avoid waiting for the entire response (from ReadAll) and monitor the stream for certain content (i.e. class='blue') then replace it with different text (i.e. class='blue-green')

How can this be done using streams efficiently and with as little allocations as possible?

user3072517
  • 513
  • 1
  • 7
  • 21
  • Try reading it to some buffer, replace string in the buffer, and then return https://golang.org/pkg/io/#MultiReader – Fominykh Maxim Nov 19 '20 at 15:41
  • It is already a reader, so I hoping to not have to buffer it; as that is still creating a copy of the data. Pipe seems to get close, but there were some issues I ran into dealing with HTML/JSON structures. I guess it would have to run through a stream parser. – user3072517 Nov 22 '20 at 17:16

0 Answers0