{"record":{"id":"4c210bda6819ba8d","repo":"cloudflare/cloudflared","slug":"400-bad-request","errorCode":null,"errorMessage":"400 Bad Request","messagePattern":"400 Bad Request","errorType":"http","errorClass":null,"httpStatus":400,"severity":"warning","filePath":"management/middleware.go","lineNumber":63,"sourceCode":"\n// Middleware validation error HTTP response JSON for returning to the eyeball\ntype managementErrorResponse struct {\n\tSuccess bool              `json:\"success,omitempty\"`\n\tErrors  []managementError `json:\"errors,omitempty\"`\n}\n\n// writeErrorResponse will respond to the eyeball with basic HTTP JSON payloads with validation failure information\nfunc writeHTTPErrorResponse(w http.ResponseWriter, errResp managementError) {\n\tw.Header().Set(\"Content-Type\", \"application/json\")\n\tw.WriteHeader(http.StatusBadRequest)\n\terr := json.NewEncoder(w).Encode(managementErrorResponse{\n\t\tSuccess: false,\n\t\tErrors:  []managementError{errResp},\n\t})\n\t// we have already written the header, so write a basic error response if unable to encode the error\n\tif err != nil {\n\t\t// fallback to text message\n\t\thttp.Error(w, fmt.Sprintf(\n\t\t\t\"%d %s\",\n\t\t\thttp.StatusBadRequest,\n\t\t\thttp.StatusText(http.StatusBadRequest),\n\t\t), http.StatusBadRequest)\n\t}\n}\n","sourceCodeStart":45,"sourceCodeEnd":70,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/management/middleware.go#L45-L70","documentation":"This is the fallback plain-text response written by writeHTTPErrorResponse in the management middleware when encoding the structured JSON error body fails. Because the HTTP status header (400) has already been written, the middleware can only emit a basic '400 Bad Request' text body. It signals a client sent a request the management API could not accept AND that the error payload itself could not be serialized.","triggerScenarios":"A client sends an invalid request to the cloudflared management API (bad JSON body or invalid query parameters); the handler writes a 400 header, and then json encoding of the managementError response fails, triggering the http.Error fallback with the literal '400 Bad Request' text.","commonSituations":"Programmatic clients POSTing malformed JSON to the management endpoint; missing required fields in management request payloads; tooling hitting the wrong endpoint shape; handcrafted curl requests with wrong content type.","solutions":["Inspect the request body/parameters sent to the management API and correct them (valid JSON, required fields present)","Set Content-Type: application/json and send a well-formed management protocol message","Use an up-to-date cloudflared client so request/response schemas match the running daemon version","Check cloudflared logs for the original error that triggered the 400 to identify the rejected field"],"exampleFix":"// before: malformed management request\n{\"unknown_field\": true}\n// after\n{\"type\": \"grpc-request\", \"body\": {\"request_type\": \"configuration\", \"version\": {}}}\n{\"capabilities\": [], \"version\": \"2023.4.1\"}","handlingStrategy":"validation","validationCode":"body, _ := json.Marshal(managementReq)\nif !json.Valid(body) {\n\treturn errors.New(\"management request payload is not valid JSON\")\n}\nreq.Header.Set(\"Content-Type\", \"application/json\")","typeGuard":null,"tryCatchPattern":"resp, err := http.Post(managementURL, \"application/json\", bytes.NewReader(body))\nif err != nil {\n\treturn err\n}\nif resp.StatusCode == http.StatusBadRequest {\n\tvar mgmtResp struct{ Errors []struct{ Code int; Message string } `json:\"errors\"` }\n\tjson.NewDecoder(resp.Body).Decode(&mgmtResp)\n\treturn fmt.Errorf(\"management API rejected request: %+v\", mgmtResp.Errors)\n}","preventionTips":["Validate request JSON against the management protocol schema before sending","Use a client matching the running cloudflared version","Parse the structured JSON errors field when present rather than the text fallback","Enable debug logging on the daemon to see why the 400 was raised"],"tags":["http","management-api","bad-request","json"],"backgroundTag":"http-error-response","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}