package handler import ( "bytes" "encoding/json" "fmt" "io" "net/http" ) func (h *Handler) Fallback(w http.ResponseWriter, r *http.Request) { h.Logger.Debug("Fallback Request received", "method", r.Method, "url", r.URL, "path", r.URL.Path, "query", r.URL.Query(), "proto", r.Proto) // Print headers for name, values := range r.Header { for _, value := range values { h.Logger.Debug("Header ", "name", name, "value", value) } } // print query for key, values := range r.URL.Query() { for _, value := range values { h.Logger.Debug("Query Paramater", "key", key, "value", value) } } body, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "Unable to read body", http.StatusBadRequest) return } defer r.Body.Close() // Pretty-print JSON to console var prettyJSON bytes.Buffer if err := json.Indent(&prettyJSON, body, "", " "); err != nil { w.Header().Set("Content-Type", "application/json") // always send 200 just so that the requestor doesn't error out w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{ "status": "ERROR", "message": fmt.Sprintf("Invalid JSON received. Visit /about for more info."), }) return } h.Logger.Debug("Received JSON payload", "payload", prettyJSON.String()) //fmt.Println("Received JSON payload:") //fmt.Println(prettyJSON.String()) // Set content type and write back the same JSON w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(prettyJSON.Bytes()) }