commit 87a78e5cfdfa506b1628a5a96a73aad21876622f Author: Aykhan Shahsuvarov Date: Thu Mar 13 03:06:04 2025 +0400 first commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ac8db0b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.aykhans.me/aykhans/reqinfo + +go 1.24.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e1f3e2e --- /dev/null +++ b/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "io" + "log" + "net/http" + "strings" +) + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Println(strings.Repeat("~", 80)) + + if len(r.Header) > 0 { + fmt.Println("--- Headers ---") + for name, values := range r.Header { + for _, value := range values { + fmt.Printf("%s: %s\n", name, value) + } + } + fmt.Println() + } + + if len(r.URL.Query()) > 0 { + fmt.Println("--- URL Parameters ---") + r.ParseForm() + for key, values := range r.URL.Query() { + for _, value := range values { + fmt.Printf("%s: %s\n", key, value) + } + } + fmt.Println() + } + + if len(r.Cookies()) > 0 { + fmt.Println("--- Cookies ---") + for _, cookie := range r.Cookies() { + fmt.Printf("%s: %s\n", cookie.Name, cookie.Value) + } + fmt.Println() + } + + bodyBytes, err := io.ReadAll(r.Body) + if err != nil { + fmt.Println("Error reading body:", err) + } else { + if bodyStr := string(bodyBytes); bodyStr != "" { + fmt.Println("--- Body ---") + fmt.Println(bodyStr) + fmt.Println() + } + } + defer r.Body.Close() + + fmt.Println("--- Client IP ---") + fmt.Println(r.RemoteAddr) +} + +func main() { + http.HandleFunc("/", handler) + fmt.Println("Server started at http://localhost:8080") + log.Fatal(http.ListenAndServe(":8080", nil)) +}