{"record":{"id":"950d18c11fac62ef","repo":"JuliusBrussee/caveman","slug":"request-body-must-contain-one-json-value","errorCode":null,"errorMessage":"request body must contain one JSON value","messagePattern":"request body must contain one JSON value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/httpx/httpx.go","lineNumber":89,"sourceCode":"\t\treturn \"Service temporarily unavailable.\"\n\tcase http.StatusGatewayTimeout:\n\t\treturn \"Upstream service timed out.\"\n\tdefault:\n\t\treturn \"Internal server error.\"\n\t}\n}\n\nfunc DecodeJSON(r *http.Request, dst any, maxBytes int64) error {\n\tdefer r.Body.Close()\n\treader := http.MaxBytesReader(nil, r.Body, maxBytes)\n\tdec := json.NewDecoder(reader)\n\tdec.DisallowUnknownFields()\n\tif err := dec.Decode(dst); err != nil {\n\t\treturn err\n\t}\n\tif err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) {\n\t\tif err == nil {\n\t\t\treturn errors.New(\"request body must contain one JSON value\")\n\t\t}\n\t\treturn err\n\t}\n\treturn nil\n}\n","sourceCodeStart":71,"sourceCodeEnd":95,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/httpx/httpx.go#L71-L95","documentation":"DecodeJSON decodes a request body into dst using encoding/json with unknown fields disallowed. After the first successful decode it requires a second decode to hit io.EOF, guaranteeing the body contains exactly one JSON value. This error is returned when the second decode succeeds (err == nil), meaning the body contained extra content after the first JSON value.","triggerScenarios":"Sending a request body with two JSON values back to back (e.g. '{\"a\":1}{\"b\":2}'), trailing garbage or whitespace-separated extra tokens after valid JSON, or a client accidentally concatenating payloads.","commonSituations":"Hand-crafted curl bodies with accidental extra characters, log/shell pipelines that append data to the body, HTTP clients that stream more than one document, or misconfigured clients that append a newline-delimited second JSON object where a single JSON document is expected.","solutions":["Remove any content after the single JSON value in the request body","If streaming NDJSON, switch to sending one JSON document per request or change the endpoint contract","Trim/validate the payload client-side before sending: json.Unmarshal on the whole body to confirm it is exactly one value","Check proxies/middleware that may append or duplicate body data"],"exampleFix":"// before\nbody := `{\"name\":\"x\"}\n{\"name\":\"y\"}`\nreq, _ := http.NewRequest(\"POST\", url, strings.NewReader(body))\n// after\nbody := `{\"name\":\"x\"}`\nreq, _ := http.NewRequest(\"POST\", url, strings.NewReader(body))","handlingStrategy":"validation","validationCode":"var probe any\nif err := json.Unmarshal(rawBody, &probe); err != nil {\n\treturn fmt.Errorf(\"body is not a single JSON value: %w\", err)\n}\n// json.Unmarshal itself enforces exactly one value","typeGuard":"func isSingleJSONValue(b []byte) bool {\n\tvar v any\n\treturn json.Unmarshal(b, &v) == nil\n}","tryCatchPattern":null,"preventionTips":["Send exactly one JSON document per request","Use a struct + json.Marshal to build bodies instead of string concatenation","Add a client-side json.Unmarshal smoke check in tests"],"tags":["json","http","validation"],"backgroundTag":"json-decode-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}