first commit

This commit is contained in:
Aykhan Shahsuvarov 2025-03-13 03:06:04 +04:00
commit 87a78e5cfd
2 changed files with 66 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.aykhans.me/aykhans/reqinfo
go 1.24.0

63
main.go Normal file
View File

@ -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))
}