Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
simple http server, just print request path and echo post payload
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
path := getPath(r.URL.Path)
fmt.Println("enter: ", path)
c, err := ioutil.ReadAll(r.Body)
if err != nil {
errMsg := fmt.Sprintf("error: %v", err)
w.Write([]byte(errMsg))
return
}
w.Write(c)
}
func getPath(w string) string {
return strings.TrimPrefix(w, "/")
}
func main() {
http.HandleFunc("/", sayHello)
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