Skip to content

Instantly share code, notes, and snippets.

@kasuganosora
Last active April 28, 2021 02:30
Show Gist options
  • Save kasuganosora/80ea91960d0f8343bfe2a397145da191 to your computer and use it in GitHub Desktop.
Save kasuganosora/80ea91960d0f8343bfe2a397145da191 to your computer and use it in GitHub Desktop.
Dump ResponseWriter
type ResponseDumpWriter struct {
Body bytes.Buffer
Headers []string
w http.ResponseWriter
statusCode int
}
func (w *ResponseDumpWriter)Header()http.Header {
return w.w.Header()
}
func (w *ResponseDumpWriter)Write(b []byte)(int, error){
w.Body.Write(b)
return w.w.Write(b)
}
func (w *ResponseDumpWriter)WriteHeader(statusCode int){
w.statusCode = statusCode
w.w.WriteHeader(statusCode)
}
func (w *ResponseDumpWriter)Dump(){
fmt.Println("Response: ")
fmt.Printf("Status code: %d\n", w.statusCode)
fmt.Println("Headers: ")
for key,values := range w.w.Header() {
fmt.Printf("Key %s, Value: %s\n", key, strings.Join(values, ", "))
}
fmt.Println("Body: ")
fmt.Println( w.Body.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment