openfaas/faas · warning

Error unmarshalling request body

Error message

Error unmarshalling request body

What it means

The scale endpoint decodes the body into types.ScaleServiceRequest with json.Unmarshal (gateway/scaling/ranges.go:54); invalid JSON yields 400 'Error unmarshalling request body'. The expected payload is a JSON object such as {"service":"fn","replicas":3}, and replicas must be a JSON number — a quoted "3" fails to decode into the int field.

Source

Thrown at gateway/scaling/ranges.go:54

		if r.Method != http.MethodPost {
			http.Error(w, "Only POST is allowed", http.StatusMethodNotAllowed)
			return
		}

		if r.Body == nil {
			http.Error(w, "Error reading request body", http.StatusBadRequest)
			return
		}

		body, err := io.ReadAll(r.Body)
		if err != nil {
			http.Error(w, "Error reading request body", http.StatusBadRequest)
			return
		}

		scaleRequest := types.ScaleServiceRequest{}
		if err := json.Unmarshal(body, &scaleRequest); err != nil {
			http.Error(w, "Error unmarshalling request body", http.StatusBadRequest)
			return
		}

		if scaleRequest.Replicas < 1 {
			scaleRequest.Replicas = 1
		}

		if scaleRequest.Replicas > DefaultMaxReplicas {
			scaleRequest.Replicas = DefaultMaxReplicas
		}

		upstreamReq, _ := json.Marshal(scaleRequest)
		// Restore the io.ReadCloser to its original state
		r.Body = io.NopCloser(bytes.NewBuffer(upstreamReq))

		next.ServeHTTP(w, r)
	}
}

View on GitHub (pinned to 8d803bf9e2)

Solutions

  1. Send well-formed JSON with a numeric replicas field: curl -X POST -H 'Content-Type: application/json' -d '{"replicas":3}' .../system/scale-function/myfn
  2. Build the body with a real JSON serializer (encoding/json, jq -n, json.dumps) instead of concatenation
  3. Validate the payload client-side (jq . or a schema check) before sending
  4. Inspect the raw request in a debug proxy to spot truncation

Example fix

# before
curl -X POST -d replicas=4 http://gw:8080/system/scale-function/myfn

# after
curl -X POST -H 'Content-Type: application/json' -d '{"replicas":4}' http://gw:8080/system/scale-function/myfn
Defensive patterns

Strategy: validation

Validate before calling

payload, err := json.Marshal(struct {
    Replicas int `json:"replicas"`
}{Replicas: 4})
if err != nil {
    return err // never hand-write the JSON body
}

Prevention

When it happens

Trigger: Posting YAML, form-encoded data (replicas=4) or plain text; truncated JSON from shell quoting mistakes; sending replicas as a string ("replicas":"3"); trailing commas or comments in hand-written JSON.

Common situations: curl -d without proper quoting on shells; CI scripts templating JSON that breaks on special characters; clients generating JSON by string concatenation instead of a serializer.

Related errors


AI-assisted analysis of openfaas/faas@8d803bf9e2 (2026-08-16). Data as JSON: /api/errors/eb0cb7005cf1e8fd. Report an issue: GitHub.