{"success":true,"name":"sending-formdata-never-set-content-type-yourself-the-boundar","description":"A file upload arrives empty or unparseable though the frontend 'sent it","category":"ai-brimind","path":"ai-brimind/sending-formdata-never-set-content-type-yourself-the-boundar/SKILL.md","file":"SKILL.md","content":"---\nname: sending-formdata-never-set-content-type-yourself-the-boundar\ndescription: \"A file upload arrives empty or unparseable though the frontend 'sent it'\"\n---\n\n# Sending FormData? Never Set Content-Type Yourself — the Boundary Must Be Auto-Generated\n\n## When to use\nFrontend code sends FormData (uploads, multipart forms) with a manually set Content-Type header, or the server receives no files/fields from a request that looks correct.\n\n## Example request\nheaders: {'Content-Type': 'multipart/form-data'} set by hand → the boundary parameter is missing, the server can't split the parts; self-sabotaged envelope. a shared API client stamps JSON content-type on everything → FormData goes out labeled as JSON; wrong label from the helpful wrapper. upload 'works' but req.file is undefined → the server-side parser never matched the malformed header; empty-handed parser. — each wrote a label the browser needed to write itself.\n\n## Instructions\nAct as a postal clerk who knows this one parcel type gets its label printed by the machine — hand-writing it, even correctly-looking, makes it undeliverable. Multipart requests carry several parts separated by a boundary string — a unique random marker the browser generates per request — and the Content-Type header must name that exact boundary so the server knows where parts begin and end. Set the header manually and you either omit the boundary or name a wrong one; either way the server's parser finds no parts, and the upload arrives 'empty' with no error pointing here. The rule: when the body is FormData, set NO Content-Type at all — the browser (and Node's fetch) detects FormData and writes the complete header, boundary included; your job is to not interfere. The realistic culprit is rarely the upload code itself — it's the shared API client that helpfully stamps a JSON content-type on every request, per the one-client pattern: teach that client to skip the header when the body is FormData. Diagnose from the wire, not the code: inspect the actual request in devtools' network tab — a healthy one shows multipart/form-data with a boundary parameter and inspectable parts; a JSON label or bare multipart label convicts the client. Server-side, ensure a multipart parser is actually mounted for the route, per the middleware-order rules — the browser's perfect envelope still needs someone who opens it. Only proceed once the request on the wire shows an auto-generated boundary and the server names each received part.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #827\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}