diff --git a/cmd/tpapi/httpTemplates.go b/cmd/tpapi/httpTemplates.go index b48c32b..4548be1 100644 --- a/cmd/tpapi/httpTemplates.go +++ b/cmd/tpapi/httpTemplates.go @@ -1,13 +1,16 @@ package main import ( - "encoding/json" "fmt" + "io" "log" "net" - "net/http" "strconv" "strings" + + "encoding/json" + "net/http" + "tplink/internal/tplink" ) @@ -198,9 +201,19 @@ func tmpltTPlinkGet(w http.ResponseWriter, r *http.Request) { func tmpltTPlinkPost(w http.ResponseWriter, r *http.Request) { var payload alertmanagerStruct - err := json.NewDecoder(r.Body).Decode(&payload) + // read body + defer r.Body.Close() + b, err := io.ReadAll(r.Body) if err != nil { - log.Printf("[TRACE] Data received: %#v\n", r.Body) + log.Printf("[DEBUG] Unable to read post body from request: %v\n", err) + tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to read post body from request: %v", err)) + return + } + + // unmarshal body + err = json.Unmarshal(b, &payload) + if err != nil { + log.Printf("[TRACE] Data received: %#v\n", string(b)) log.Printf("[INFO] Unable to parse payload from API call: %v\n", err) tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to read info from device: %v\n", err)) return