{"record":{"id":"03744574c21f851c","repo":"gofiber/fiber","slug":"write-formdata-error-w","errorCode":null,"errorMessage":"write formdata error: %w","messagePattern":"write formdata error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/hooks.go","lineNumber":267,"sourceCode":"\tif err := mw.Close(); err != nil {\n\t\treturn fmt.Errorf(\"failed to close multipart writer: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// writeMultipartBody writes the form fields and files of req to mw.\nfunc writeMultipartBody(mw *multipart.Writer, req *Request) error {\n\t// Add form data.\n\tvar err error\n\tfor key, value := range req.formData.All() {\n\t\terr = mw.WriteField(utils.UnsafeString(key), utils.UnsafeString(value))\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write formdata error: %w\", err)\n\t}\n\n\t// Add files.\n\tfileBuf, ok := fileBufPool.Get().(*[]byte)\n\tif !ok {\n\t\treturn errSyncPoolBuffer\n\t}\n\n\tdefer fileBufPool.Put(fileBuf)\n\n\tfor i, f := range req.files {\n\t\tif f.name == \"\" && f.path == \"\" {\n\t\t\treturn ErrFileNoName\n\t\t}\n\n\t\t// Set the file name if not provided.\n\t\tif f.name == \"\" && f.path != \"\" {\n\t\t\tf.path = filepath.Clean(f.path)","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/client/hooks.go#L249-L285","documentation":"writeMultipartBody iterates req.formData and calls mw.WriteField for each key/value. This wraps the first WriteField failure, which comes from the underlying body writer rejecting bytes (encoding issue, writer closed, buffer overflow).","triggerScenarios":"A formData value containing characters the multipart writer cannot encode; the underlying BodyWriter was closed or errored before fields are written; a pipe writer whose read end is gone.","commonSituations":"Calling req.formData.Add with a value that is not a valid string for the writer; reusing a Request whose RawRequest body was already consumed/closed; concurrent writes to the same body.","solutions":["Inspect the wrapped cause to determine if it is an encoding or writer-lifecycle error.","Do not reuse or close the request body before the send completes; let the client own body lifetime.","Sanitize formData values (strip control bytes) if they originate from untrusted input."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := c.Post(url, req); err != nil && strings.Contains(err.Error(), \"write formdata\") {\n    log.Printf(\"multipart field write failed: %v\", err)\n}","preventionTips":["Strip control bytes from formData values sourced from untrusted input.","Never reuse a Request whose body was already consumed or closed.","Avoid concurrent writes to the same RawRequest body."],"tags":["client","multipart","form-data","upload","io"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}