Skip to content

Instantly share code, notes, and snippets.

@tschak909
Created February 8, 2021 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschak909/5af74fec0e49c6071d924e7cb0b15680 to your computer and use it in GitHub Desktop.
Save tschak909/5af74fec0e49c6071d924e7cb0b15680 to your computer and use it in GitHub Desktop.
Echoes GET and POST requests.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
for k, v := range r.URL.Query() {
fmt.Printf("%s: %s\n",k, v)
}
w.Write([]byte("Received a GET request\n"))
case "POST":
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", reqBody)
w.Write([]byte("Received a POST request\n"))
default:
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte(http.StatusText(http.StatusNotImplemented)))
}
}
func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment