Skip to content

Instantly share code, notes, and snippets.

@arturo-source
Last active August 4, 2025 12:52
Show Gist options
  • Save arturo-source/473315f197dade72d6ac52e6ad1ebc5e to your computer and use it in GitHub Desktop.
Save arturo-source/473315f197dade72d6ac52e6ad1ebc5e to your computer and use it in GitHub Desktop.
Go HTTP server to show if client request is working as expected.
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func defaultHandler(res http.ResponseWriter, req *http.Request) {
body, err := io.ReadAll(req.Body)
if err != nil {
fmt.Fprintf(res, "error parsing body : %s", err)
return
}
response := req.Method + " " + req.URL.Path
response += "\nheaders: " + fmt.Sprintf("%v", req.Header)
response += "\nbody: " + string(body)
fmt.Println(response)
fmt.Fprintf(res, response)
}
func main() {
http.HandleFunc("/", defaultHandler)
port := "8080"
if len(os.Args) > 1 {
port = os.Args[1]
}
fmt.Print("listening on http://localhost:", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment