gofiber/fiber · critical
boundary generation: %w
Error message
boundary generation: %w
What it means
Returned in parserRequestHeader (client/hooks.go:153) when unsafeRandString fails while generating the random suffix appended to the default multipart boundary for a filesBody request. It is a thin wrapper around the underlying rand.Read failure (errors 84/85). The boundary is only auto-generated when the request still uses the default boundary constant 'FiberFormBoundary'.
Source
Thrown at client/hooks.go:153
// Set Content-Type and Accept headers based on the request body type.
switch req.bodyType {
case jsonBody:
req.RawRequest.Header.SetContentType(applicationJSON)
req.RawRequest.Header.Set(headerAccept, applicationJSON)
case xmlBody:
req.RawRequest.Header.SetContentType(applicationXML)
case cborBody:
req.RawRequest.Header.SetContentType(applicationCBOR)
case formBody:
req.RawRequest.Header.SetContentType(applicationForm)
case filesBody:
req.RawRequest.Header.SetContentType(multipartFormData)
// If boundary is default, append a random string to it.
if req.boundary == boundary {
randStr, err := unsafeRandString(16)
if err != nil {
return fmt.Errorf("boundary generation: %w", err)
}
req.boundary += randStr
}
req.RawRequest.Header.SetMultipartFormBoundary(req.boundary)
default:
// noBody or rawBody do not require special handling here.
}
// Set User-Agent header.
req.RawRequest.Header.SetUserAgent(defaultUserAgent)
if c.userAgent != "" {
req.RawRequest.Header.SetUserAgent(c.userAgent)
}
if req.userAgent != "" {
req.RawRequest.Header.SetUserAgent(req.userAgent)
}
// Set Referer header.View on GitHub (pinned to 9a4c7e57fe)
Solutions
- Fix the environment so crypto/rand works (expose /dev/urandom, allow getrandom).
- As a workaround, set an explicit boundary via Request.SetBoundary so unsafeRandString is skipped.
- Treat the error as fatal/infra-level — retrying on the same host will keep failing.
Example fix
// before — relies on auto-generated boundary, fails without /dev/urandom
client.R().SetFiles("./a.txt").Get(url)
// after — set a boundary explicitly to bypass unsafeRandString
client.R().SetBoundary("MyAppBoundary123").SetFiles("./a.txt").Get(url) Defensive patterns
Strategy: fallback
Validate before calling
// Detect CSPRNG unavailability before attempting uploads.
func canGenerateBoundary() bool {
return rand.Read(make([]byte, 1)) == nil
} Try / catch
resp, err := client.R().SetFiles(f).Get(url)
if err != nil && strings.Contains(err.Error(), "boundary generation") {
// rand unavailable — retry with an explicit boundary
resp, err = client.R().SetBoundary("ManualBoundary").SetFiles(f).Get(url)
} Prevention
- Ensure /dev/urandom is available in the deployment environment.
- Set an explicit boundary via Request.SetBoundary when randomness cannot be guaranteed.
- Treat boundary-generation errors as infrastructure faults, not transient network errors.
When it happens
Trigger: Sending a multipart file upload (Request.SetFiles) without a custom boundary, on a host where crypto/rand.Read fails. The header-preparation hook tries to build boundary = 'FiberFormBoundary' + randomString(16) and returns this wrapped error.
Common situations: Same CSPRNG-unavailability contexts as errors 84/85 (locked-down container, missing /dev/urandom, blocked getrandom syscall), specifically manifesting during the header-building phase of a file upload.
Related errors
- rand.Read failed: %w
- set boundary error: %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/a2f6e24f7a5f60cc.json.
Report an issue: GitHub.