{"record":{"id":"8b2d4c1d067f0874","repo":"valyala/fasthttp","slug":"form-boundary-cannot-be-empty","errorCode":null,"errorMessage":"form boundary cannot be empty","messagePattern":"form boundary cannot be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":1225,"sourceCode":"\n\treturn req.multipartForm, nil\n}\n\nfunc marshalMultipartForm(f *multipart.Form, boundary string) ([]byte, error) {\n\tvar buf bytebufferpool.ByteBuffer\n\tif err := WriteMultipartForm(&buf, f, boundary); err != nil {\n\t\treturn nil, err\n\t}\n\treturn buf.B, nil\n}\n\n// WriteMultipartForm writes the given multipart form f with the given\n// boundary to w.\nfunc WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {\n\t// Do not care about memory allocations here, since multipart\n\t// form processing is slow.\n\tif boundary == \"\" {\n\t\treturn errors.New(\"form boundary cannot be empty\")\n\t}\n\n\tmw := multipart.NewWriter(w)\n\tif err := mw.SetBoundary(boundary); err != nil {\n\t\treturn fmt.Errorf(\"cannot use form boundary %q: %w\", boundary, err)\n\t}\n\n\t// marshal values\n\tfor k, vv := range f.Value {\n\t\tfor _, v := range vv {\n\t\t\tif err := mw.WriteField(k, v); err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot write form field %q value %q: %w\", k, v, err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// marshal files\n\tfor k, fvv := range f.File {","sourceCodeStart":1207,"sourceCodeEnd":1243,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L1207-L1243","documentation":"WriteMultipartForm serializes a multipart.Form to a writer using the supplied boundary string. An empty boundary is invalid — multipart.NewWriter.SetBoundary would reject it — so the function fails fast with this error before writing anything.","triggerScenarios":"Calling fasthttp.WriteMultipartForm(w, form, \"\") with an empty boundary, typically when the boundary was taken from a request Content-Type that had none, or an unset variable.","commonSituations":"Proxying/rewriting multipart bodies after extracting a boundary from a Content-Type header that lacked one; generating responses from parsed forms where the original boundary was lost.","solutions":["Pass a non-empty boundary, e.g. one generated with multipart.NewWriter(...).Boundary() or random.Boundary().","Extract the boundary with mime.ParseMediaType from the incoming Content-Type and validate it's non-empty before calling.","If re-emitting a parsed form, create a fresh multipart.Writer and use its boundary.","Wrap the call and return 400/500 with a clear message when the boundary is missing."],"exampleFix":"// before\nerr := fasthttp.WriteMultipartForm(w, form, boundaryFromHeader) // \"\"\n// after\nmw := multipart.NewWriter(io.Discard)\nboundary := mw.Boundary() // non-empty random boundary\nerr := fasthttp.WriteMultipartForm(w, form, boundary)","handlingStrategy":"validation","validationCode":"if boundary == \"\" {\n    return errors.New(\"multipart boundary required\")\n}\n// or generate one:\n// b := multipart.NewWriter(io.Discard).Boundary()","typeGuard":null,"tryCatchPattern":"if err := fasthttp.WriteMultipartForm(w, form, boundary); err != nil {\n    // regenerate a random boundary and retry once\n}","preventionTips":["Never pass a boundary extracted from a header without checking it's non-empty","Generate boundaries with multipart.NewWriter().Boundary()","Validate with mime.ParseMediaType when reusing inbound boundaries"],"tags":["http","multipart","boundary","fasthttp"],"backgroundTag":"multipart-empty-boundary","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}