Created
February 8, 2021 00:30
-
-
Save tschak909/5af74fec0e49c6071d924e7cb0b15680 to your computer and use it in GitHub Desktop.
Echoes GET and POST requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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