Created
May 31, 2024 05:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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