Skip to content

Instantly share code, notes, and snippets.

@arturo-source
Last active May 2, 2023 12:30
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/ioutil"
"net/http"
"os"
)
func defaultHandler(res http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
fmt.Fprintf(res, "error parsing body : %s", err)
return
}
response := req.Method + " " + req.URL.Path
response += "\nbody: " + string(body)
response += "\nheaders: " + fmt.Sprintf("%v", req.Header)
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