{"record":{"id":"00df88c22dd109bf","repo":"kubernetes/kops","slug":"invalid-request-body","errorCode":null,"errorMessage":"Invalid request body","messagePattern":"Invalid request body","errorType":"http","errorClass":null,"httpStatus":400,"severity":"warning","filePath":"discovery/pkg/discovery/server.go","lineNumber":156,"sourceCode":"\t\tTypeMeta: metav1.TypeMeta{Kind: \"DiscoveryEndpointList\", APIVersion: \"discovery.kops.k8s.io/v1alpha1\"},\n\t}\n\n\tfor _, ep := range endpoints {\n\t\tif ns == \"\" || ep.ObjectMeta.Namespace == ns {\n\t\t\tresp.Items = append(resp.Items, *ep)\n\t\t}\n\t}\n\n\ts.writeJSON(w, http.StatusOK, resp)\n}\n\nfunc (s *Server) handleCreateDiscoveryEndpoint(w http.ResponseWriter, r *http.Request, userInfo *UserInfo) {\n\tuniverseID := r.PathValue(\"universe\")\n\tns := r.PathValue(\"namespace\")\n\n\tvar input api.DiscoveryEndpoint\n\tif err := json.NewDecoder(r.Body).Decode(&input); err != nil {\n\t\thttp.Error(w, \"Invalid request body\", http.StatusBadRequest)\n\t\treturn\n\t}\n\n\t// Validation: ensure the name matches the clientID from the cert\n\tif input.ObjectMeta.Name != \"\" && input.ObjectMeta.Name != userInfo.ClientID {\n\t\thttp.Error(w, fmt.Sprintf(\"Forbidden: cannot register node name '%s' with client cert '%s'\", input.ObjectMeta.Name, userInfo.ClientID), http.StatusForbidden)\n\t\treturn\n\t}\n\n\t// Validation: ensure the namespace in body matches the URL\n\tif input.ObjectMeta.Namespace != ns {\n\t\thttp.Error(w, \"Forbidden: namespace does not match\", http.StatusForbidden)\n\t\treturn\n\t}\n\n\tif err := s.Store.UpsertDiscoveryEndpoint(r.Context(), universeID, &input); err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Error creating endpoint: %v\", err), http.StatusInternalServerError)\n\t\treturn","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/discovery/pkg/discovery/server.go#L138-L174","documentation":"handleCreateDiscoveryEndpoint decodes the POST body into api.DiscoveryEndpoint with encoding/json. If the body is not valid JSON or does not match the DiscoveryEndpoint schema, decoding fails and the server returns 400 'Invalid request body'. The detailed decode error is not included in the response.","triggerScenarios":"POST to /{universe}/apis/discovery.kops.k8s.io/v1alpha1/namespaces/{namespace}/discoveryendpoints with: empty body, malformed JSON (syntax errors), wrong Content-Type handling, YAML instead of JSON, or fields of the wrong type.","commonSituations":"Clients sending YAML manifests unconverted; truncated bodies; template rendering bugs producing invalid JSON; sending a List where an item is expected; type mismatches (string vs int in spec).","solutions":["Validate the JSON body with a parser/linter before sending (e.g. jq, python -m json.tool).","Confirm you are sending a single DiscoveryEndpoint object, not YAML or a List.","Set Content-Type: application/json and ensure the full body is transmitted.","Unmarshal locally against api.DiscoveryEndpoint to see the exact decode error Go would return."],"exampleFix":"// before: sending YAML\nbody := []byte(\"metadata:\\n  name: node1\\n\")\n// after: valid JSON\nbody := []byte(`{\"metadata\":{\"name\":\"node1\",\"namespace\":\"default\"}}`)","handlingStrategy":"validation","validationCode":"var probe interface{}\nif err := json.Unmarshal(body, &probe); err != nil {\n    return fmt.Errorf(\"body is not valid JSON: %w\", err)\n}\nvar ep api.DiscoveryEndpoint\nif err := json.Unmarshal(body, &ep); err != nil {\n    return fmt.Errorf(\"body does not match DiscoveryEndpoint schema: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"resp, err := client.Post(url, \"application/json\", bytes.NewReader(body))\nif err == nil && resp.StatusCode == http.StatusBadRequest {\n    return fmt.Errorf(\"server rejected body; validate JSON with: json.Unmarshal(body, &DiscoveryEndpoint{})\")\n}","preventionTips":["Validate JSON with jq or json.tool before sending","Convert YAML to JSON before POSTing","Set Content-Type: application/json","Locally decode into api.DiscoveryEndpoint in tests before deploy"],"tags":["http","json","validation","bad-request"],"backgroundTag":"invalid-json-body","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}