JuliusBrussee/caveman · error
request body must contain one JSON value
Error message
request body must contain one JSON value
What it means
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.
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
Example fix
// before
body := `{"name":"x"}
{"name":"y"}`
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
// after
body := `{"name":"x"}`
req, _ := http.NewRequest("POST", url, strings.NewReader(body)) Defensive patterns
Strategy: validation
Validate before calling
var probe any
if err := json.Unmarshal(rawBody, &probe); err != nil {
return fmt.Errorf("body is not a single JSON value: %w", err)
}
// json.Unmarshal itself enforces exactly one value Type guard
func isSingleJSONValue(b []byte) bool {
var v any
return json.Unmarshal(b, &v) == nil
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- assembly slot content is not JSON-serializable
- cave_output_schema_mismatch
- cave response was not valid JSON
- device authorization failed
- duplicate object key
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/950d18c11fac62ef.
Report an issue: GitHub.
Appendix: source
Thrown at shared/platform/httpx/httpx.go:89
return "Service temporarily unavailable."
case http.StatusGatewayTimeout:
return "Upstream service timed out."
default:
return "Internal server error."
}
}
func DecodeJSON(r *http.Request, dst any, maxBytes int64) error {
defer r.Body.Close()
reader := http.MaxBytesReader(nil, r.Body, maxBytes)
dec := json.NewDecoder(reader)
dec.DisallowUnknownFields()
if err := dec.Decode(dst); err != nil {
return err
}
if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
if err == nil {
return errors.New("request body must contain one JSON value")
}
return err
}
return nil
}
View on GitHub (pinned to 3ee70a1026)