openfaas/faas · error
A body is required for this endpoint
Error message
A body is required for this endpoint
What it means
MakeAlertHandler serves POST /system/alert, the webhook endpoint Prometheus Alertmanager calls so the gateway can scale functions from zero. The handler rejects any request whose r.Body is nil with 400 Bad Request before parsing the alert JSON. Go's net/http server always supplies a non-nil body for real requests, so this branch is a defensive guard: an empty body passes it and fails later at JSON decoding.
Source
Thrown at gateway/handlers/alerthandler.go:26
import (
"encoding/json"
"fmt"
"io"
"log"
"math"
"net/http"
"github.com/openfaas/faas/gateway/pkg/middleware"
"github.com/openfaas/faas/gateway/requests"
"github.com/openfaas/faas/gateway/scaling"
)
// MakeAlertHandler handles alerts from Prometheus Alertmanager
func MakeAlertHandler(service scaling.ServiceQuery, defaultNamespace string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "A body is required for this endpoint", http.StatusBadRequest)
return
}
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Unable to read alert."))
log.Println(err)
return
}
var req requests.PrometheusAlert
if err := json.Unmarshal(body, &req); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Unable to parse alert, bad format."))View on GitHub (pinned to 8d803bf9e2)
Solutions
- Send a JSON alert payload: curl -X POST -H 'Content-Type: application/json' -d '{"status":"firing","alerts":[]}' http://gateway:8080/system/alert
- Verify the Alertmanager receiver URL points at the gateway's /system/alert path
- Make sure no proxy in front of the gateway strips request bodies
- In Go tests, build requests with http.NewRequest and a bytes.Reader body
Example fix
# before
curl -X POST http://gateway:8080/system/alert
# after
curl -X POST -H 'Content-Type: application/json' \
-d '{"status":"firing","alerts":[]}' \
http://gateway:8080/system/alert Defensive patterns
Strategy: validation
Validate before calling
// client-side: never call /system/alert without a payload
if len(alertPayload) == 0 {
return errors.New("alert payload is required")
}
req, _ := http.NewRequest(http.MethodPost, gatewayURL+"/system/alert", bytes.NewReader(alertPayload)) Prevention
- Test Alertmanager receivers with amtool or curl including a real JSON body
- Add a smoke test that POSTs a minimal firing alert and expects 200
- Never probe the endpoint with an empty body
When it happens
Trigger: Invoking POST /system/alert with no payload at all, e.g. curl -X POST http://gateway:8080/system/alert without a -d flag; unit tests that pass a bare &http.Request{} with a nil Body directly to the handler.
Common situations: Manually probing the endpoint to verify Alertmanager wiring and forgetting the payload; custom health checks that POST without a body; hand-rolled scripts forwarding alerts without copying the request body.
Related errors
- err.Error()
- invalid value for max_idle_conns: %s
- invalid value for max_idle_conns_per_host: %s
- log request failed
- unknown log request error (%v)
AI-assisted analysis of openfaas/faas@8d803bf9e2 (2026-08-16).
Data as JSON: /api/errors/3bd5fe16f620c1b8.
Report an issue: GitHub.