{"id":"9303257bd3c31df9","repo":"gofiber/fiber","slug":"set-boundary-error-w","errorCode":null,"errorMessage":"set boundary error: %w","messagePattern":"set boundary error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/hooks.go","lineNumber":239,"sourceCode":"\t\tif body, ok := req.body.([]byte); ok { //nolint:revive // ignore simplicity\n\t\t\treq.RawRequest.SetBody(body)\n\t\t} else {\n\t\t\treturn ErrBodyType\n\t\t}\n\tcase noBody:\n\t\t// No body to set.\n\t\treturn nil\n\tdefault:\n\t\treturn ErrBodyTypeNotSupported\n\t}\n\treturn nil\n}\n\n// parserRequestBodyFile handles the case where the request contains files to be uploaded.\nfunc parserRequestBodyFile(req *Request) error {\n\tmw := multipart.NewWriter(req.RawRequest.BodyWriter())\n\tif err := mw.SetBoundary(req.boundary); err != nil {\n\t\treturn fmt.Errorf(\"set boundary error: %w\", err)\n\t}\n\n\tif err := writeMultipartBody(mw, req); err != nil {\n\t\tmw.Close() //nolint:errcheck // the body write already failed; surface that error instead\n\t\treturn err\n\t}\n\n\t// Close writes the trailing boundary; if it fails the multipart body is\n\t// incomplete, so surface the error instead of sending a malformed request.\n\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 {","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/hooks.go#L221-L257","documentation":"Returned in parserRequestBodyFile (client/hooks.go:239) when mime/multipart.Writer.SetBoundary rejects req.boundary. SetBoundary enforces RFC 2046: the boundary must be 1–70 characters, contain only the tspecials-safe set (alphanumeric and ' ( ) + _ , - . / : = ?), and contain no whitespace or the boundary delimiter itself. A user-supplied boundary violating these rules triggers the error.","triggerScenarios":"Calling Request.SetBoundary with an invalid value — e.g. \"*\" (the test-suite reproducer at request_test.go:1556), a string longer than 70 chars, one containing spaces/quotes/control chars, or an empty string. The error appears at request-build time for a filesBody (file upload) request.","commonSituations":"Generating a boundary from an unsafe charset; hardcoding a boundary with special characters; a boundary derived from user input that includes spaces or symbols; copying a boundary from a Content-Type header that already includes quotes or parameters.","solutions":["Use a boundary of 1–70 alphanumeric characters plus at most the allowed specials ( - . _ + / : = ? ).","Prefer letting the client auto-generate the boundary (omit SetBoundary) so it is always valid.","Sanitize any user-supplied boundary with a regexp like ^[A-Za-z0-9'()+_,.\\-/:=?]{1,70}$ before calling SetBoundary."],"exampleFix":"// before — invalid boundary characters\nreq.SetBoundary(\"* * *\")\n\n// after — valid boundary, or omit to auto-generate\nreq.SetBoundary(\"----MyAppBoundary0123456789\")\n// or simply: do not call SetBoundary at all","handlingStrategy":"validation","validationCode":"// Validate a boundary against RFC 2046 rules used by mime/multipart.\nvar boundaryRe = regexp.MustCompile(`^[A-Za-z0-9'()+_,.\\-/:=?]{1,70}$`)\nfunc validBoundary(b string) bool { return boundaryRe.MatchString(b) }","typeGuard":"func validBoundary(b string) bool {\n    return boundaryRe.MatchString(b)\n}","tryCatchPattern":null,"preventionTips":["Prefer auto-generated boundaries (omit SetBoundary) so validity is guaranteed.","Restrict custom boundaries to 1–70 alphanumeric characters plus the allowed specials.","Sanitize any user-supplied boundary before passing it to SetBoundary."],"tags":["client","multipart","boundary","validation"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}