gofiber/fiber · error
set boundary error: %w
Error message
set boundary error: %w
What it means
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.
Source
Thrown at client/hooks.go:239
if body, ok := req.body.([]byte); ok { //nolint:revive // ignore simplicity
req.RawRequest.SetBody(body)
} else {
return ErrBodyType
}
case noBody:
// No body to set.
return nil
default:
return ErrBodyTypeNotSupported
}
return nil
}
// parserRequestBodyFile handles the case where the request contains files to be uploaded.
func parserRequestBodyFile(req *Request) error {
mw := multipart.NewWriter(req.RawRequest.BodyWriter())
if err := mw.SetBoundary(req.boundary); err != nil {
return fmt.Errorf("set boundary error: %w", err)
}
if err := writeMultipartBody(mw, req); err != nil {
mw.Close() //nolint:errcheck // the body write already failed; surface that error instead
return err
}
// Close writes the trailing boundary; if it fails the multipart body is
// incomplete, so surface the error instead of sending a malformed request.
if err := mw.Close(); err != nil {
return fmt.Errorf("failed to close multipart writer: %w", err)
}
return nil
}
// writeMultipartBody writes the form fields and files of req to mw.
func writeMultipartBody(mw *multipart.Writer, req *Request) error {View on GitHub (pinned to 9a4c7e57fe)
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.
Example fix
// before — invalid boundary characters
req.SetBoundary("* * *")
// after — valid boundary, or omit to auto-generate
req.SetBoundary("----MyAppBoundary0123456789")
// or simply: do not call SetBoundary at all Defensive patterns
Strategy: validation
Validate before calling
// Validate a boundary against RFC 2046 rules used by mime/multipart.
var boundaryRe = regexp.MustCompile(`^[A-Za-z0-9'()+_,.\-/:=?]{1,70}$`)
func validBoundary(b string) bool { return boundaryRe.MatchString(b) } Type guard
func validBoundary(b string) bool {
return boundaryRe.MatchString(b)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- boundary generation: %w
- rand.Read failed: %w
- failed to close multipart writer: %w
- write formdata error: %w
- create file error: %w
AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04).
Data as JSON: /data/errors/9303257bd3c31df9.json.
Report an issue: GitHub.