Last active
May 2, 2023 12:30
-
-
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.
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" | |
"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