{"record":{"id":"9cd661f1b75f6de5","repo":"uber-go/zap","slug":"malformed-request-body-v","errorCode":null,"errorMessage":"malformed request body: %v","messagePattern":"malformed request body: (.+?)","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"http_handler.go","lineNumber":137,"sourceCode":"\nfunc decodePutURL(r *http.Request) (zapcore.Level, error) {\n\tlvl := r.FormValue(\"level\")\n\tif lvl == \"\" {\n\t\treturn 0, errors.New(\"must specify logging level\")\n\t}\n\tvar l zapcore.Level\n\tif err := l.UnmarshalText([]byte(lvl)); err != nil {\n\t\treturn 0, err\n\t}\n\treturn l, nil\n}\n\nfunc decodePutJSON(body io.Reader) (zapcore.Level, error) {\n\tvar pld struct {\n\t\tLevel *zapcore.Level `json:\"level\"`\n\t}\n\tif err := json.NewDecoder(body).Decode(&pld); err != nil {\n\t\treturn 0, fmt.Errorf(\"malformed request body: %v\", err)\n\t}\n\tif pld.Level == nil {\n\t\treturn 0, errors.New(\"must specify logging level\")\n\t}\n\treturn *pld.Level, nil\n}\n","sourceCodeStart":119,"sourceCodeEnd":144,"githubUrl":"https://github.com/uber-go/zap/blob/bbd4ecbd8760678fdbac94c93d9aca8069c83b2f/http_handler.go#L119-L144","documentation":"decodePutJSON parses the JSON body of a PUT request to zap's HTTP level handler and returns this error when the body cannot be decoded as JSON. The underlying decoder error is wrapped into the message. It indicates the request payload is not valid JSON for the expected shape {\"level\": ...}.","triggerScenarios":"Sending a PUT request to the /log/level endpoint with an empty body, non-JSON content (e.g. form-encoded or plain text), or syntactically invalid JSON such as 'level=debug' without quotes.","commonSituations":"curl calls without -H 'Content-Type: application/json' and without a properly quoted JSON body; automation scripts using key=value syntax; truncation of large bodies by proxies.","solutions":["Send a valid JSON body, e.g. curl -X PUT -d '{\"level\":\"info\"}' http://host:port/log/level.","Set the Content-Type: application/json header on the request.","Validate the JSON payload client-side (e.g. json.Valid in Go or JSON.parse in JS) before sending."],"exampleFix":"// before\ncurl -X PUT http://localhost:8080/log/level -d 'level=debug'\n// after\ncurl -X PUT http://localhost:8080/log/level -H 'Content-Type: application/json' -d '{\"level\":\"debug\"}'","handlingStrategy":"validation","validationCode":"body, _ := io.ReadAll(resp)\nif !json.Valid(body) {\n    return fmt.Errorf(\"invalid JSON payload for /log/level\")\n}","typeGuard":null,"tryCatchPattern":"resp, err := http.Put(url, \"application/json\", strings.NewReader(`{\"level\":\"info\"}`))\nif err != nil || resp.StatusCode != http.StatusOK {\n    // read body and inspect 'malformed request body' before retrying\n}","preventionTips":["Always send Content-Type: application/json with a JSON object body","Validate payloads with json.Valid or JSON.parse before sending","Use a typed client wrapper that marshals a struct instead of hand-built strings"],"tags":["http","json","zap","request-body"],"backgroundTag":"malformed-json-body","analyzedSha":"bbd4ecbd8760678fdbac94c93d9aca8069c83b2f","analyzedAt":"2026-08-31T13:01:40.228Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}