Skip to content

Instantly share code, notes, and snippets.

@amir-khassaia
Created December 3, 2021 01:08
Show Gist options
  • Save amir-khassaia/474e1882023aec9eb6fbfd8c5d0e5bf3 to your computer and use it in GitHub Desktop.
Save amir-khassaia/474e1882023aec9eb6fbfd8c5d0e5bf3 to your computer and use it in GitHub Desktop.
Simple GoLang HTTP REST endpoint that dumps request bodies to stdout
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
)
const path = "logs"
const port = 3003
func handleRequests() {
fmt.Printf("Listening on: http://:%d/%s\n", port, path)
router := mux.NewRouter().StrictSlash(true)
router.Methods("POST").Path("/" + path).HandlerFunc(logs)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), router))
}
func logs(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
log.Printf("failed to read body, Error : %v\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
_ = r.Body.Close()
log.Printf("%s\n", string(body))
}
func main() {
handleRequests()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment