Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Created June 11, 2019 21:28
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 dmerrick/30e1750ce17f1f5a395968f5943d18c9 to your computer and use it in GitHub Desktop.
Save dmerrick/30e1750ce17f1f5a395968f5943d18c9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func handle(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
fmt.Fprintf(w, "Perhaps you meant to make a POST request?\n")
case "POST":
b, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(b) + "\n")
fmt.Fprintf(w, "OK\n")
default:
fmt.Fprintf(w, "Only GET and POST methods are supported.\n")
}
}
func main() {
http.HandleFunc("/", handle)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment