Skip to content

Instantly share code, notes, and snippets.

@krashanoff
Created May 31, 2024 05:57
Show Gist options
  • Save krashanoff/3294519b495eabf8c26eac53cec180ad to your computer and use it in GitHub Desktop.
Save krashanoff/3294519b495eabf8c26eac53cec180ad to your computer and use it in GitHub Desktop.
HTTP server to echo back your HTTP request body in Base64. Useful for writing AWS Lambda tests.
package main
import (
"encoding/base64"
"io"
"net/http"
"os"
)
type EchoReader struct {
io.Reader
}
func (e EchoReader) Read(p []byte) (n int, err error) {
n, err = e.Reader.Read(p)
os.Stdout.Write(p)
return
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
reader := EchoReader{r.Body}
encoder := base64.NewEncoder(base64.RawStdEncoding, w)
io.Copy(encoder, reader)
})
http.ListenAndServe("0.0.0.0:5454", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment