{"record":{"id":"15179550f14024f9","repo":"plandex-ai/plandex","slug":"error-parsing-request-body-151795","errorCode":null,"errorMessage":"Error parsing request body","messagePattern":"Error parsing request body","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"app/server/handlers/projects.go","lineNumber":37,"sourceCode":"\n\tauth := Authenticate(w, r, true)\n\tif auth == nil {\n\t\treturn\n\t}\n\n\t// read the request body\n\tbody, err := io.ReadAll(r.Body)\n\tif err != nil {\n\t\tlog.Printf(\"Error reading request body: %v\\n\", err)\n\t\thttp.Error(w, \"Error reading request body\", http.StatusInternalServerError)\n\t\treturn\n\t}\n\tdefer r.Body.Close()\n\n\tvar requestBody shared.CreateProjectRequest\n\tif err := json.Unmarshal(body, &requestBody); err != nil {\n\t\tlog.Printf(\"Error parsing request body: %v\\n\", err)\n\t\thttp.Error(w, \"Error parsing request body\", http.StatusBadRequest)\n\t\treturn\n\t}\n\n\tif requestBody.Name == \"\" {\n\t\tlog.Println(\"Received empty name field\")\n\t\thttp.Error(w, \"name field is required\", http.StatusBadRequest)\n\t\treturn\n\t}\n\n\tvar projectId string\n\terr = db.WithTx(r.Context(), \"create project\", func(tx *sqlx.Tx) error {\n\t\tvar err error\n\n\t\tprojectId, err = db.CreateProject(auth.OrgId, requestBody.Name, tx)\n\n\t\tif err != nil {\n\t\t\tlog.Printf(\"Error creating project: %v\\n\", err)\n\t\t\treturn fmt.Errorf(\"error creating project: %v\", err)","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/projects.go#L19-L55","documentation":"CreateProjectHandler unmarshals the read body into shared.CreateProjectRequest; on JSON parse failure it returns 400 with 'Error parsing request body'. The body arrived intact but is not valid JSON for the CreateProjectRequest shape (a follow-up 'name field is required' 400 covers valid JSON with an empty name).","triggerScenarios":"json.Unmarshal(body, &requestBody) fails at projects.go:37 — malformed JSON, HTML error pages forwarded by a proxy, or mismatched field types versus shared.CreateProjectRequest.","commonSituations":"Posting FormData/multipart instead of JSON, double-encoding the JSON string, an auth redirect returning an HTML page to the API route, or client/server schema version mismatch.","solutions":["Confirm the payload parses: jq . payload.json.","Send 'Content-Type: application/json' with a raw JSON object body, not a stringified-string or form fields.","Align field names/types with the shared.CreateProjectRequest struct of the deployed server.","If behind a proxy, verify the API route is not intercepted by an auth/login HTML redirect."],"exampleFix":"// before\nfetch(url, {method:'POST', body: new URLSearchParams({name:'proj'})})  // form-encoded\n// after\nfetch(url, {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({name:'proj'})})","handlingStrategy":"validation","validationCode":"// validate before POST /projects\nif name == \"\" { return errors.New(\"name is required\") }\npayload, err := json.Marshal(shared.CreateProjectRequest{Name: name})\nif err != nil { return err }\nvar check shared.CreateProjectRequest\nif json.Unmarshal(payload, &check) != nil { return errors.New(\"payload is not valid CreateProjectRequest JSON\") }","typeGuard":"func isValidCreateProjectRequest(b []byte) bool {\n    var req shared.CreateProjectRequest\n    return json.Unmarshal(b, &req) == nil\n}","tryCatchPattern":"resp, err := postJSON(projectsURL, payload)\nif err != nil {\n    if resp != nil && resp.StatusCode == 400 && bodyContains(resp, \"Error parsing request body\") {\n        return fmt.Errorf(\"create-project payload must be JSON with a 'name' field\")\n    }\n    return err\n}","preventionTips":["Send raw JSON objects, never FormData or double-encoded strings","Set Content-Type: application/json on every project API call","Verify proxies do not inject HTML (login pages) into API responses you parse","Keep client request structs aligned with shared.CreateProjectRequest"],"tags":["http","json","go","validation"],"backgroundTag":"invalid-json-in-request","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}