gofiber/fiber · error

the body type should be []byte

Error message

the body type should be []byte

What it means

Returned in parserRequestBody (client/hooks.go:224) when the request body type was tagged rawBody but the value stored in req.body is not []byte. The raw-body path is the only one that does a concrete type assertion, so passing a string, struct, or any other type with the raw marker trips it.

Solutions

  1. Always pass []byte to the raw body setter: req.Body([]byte("payload")).
  2. If you have a string, convert first: req.Body([]byte(s)).
  3. Use the typed setters (JSON, XML, Form) for structured payloads — they handle serialization and set the correct bodyType.
  4. Add a unit test that asserts req.body has type []byte after your custom request-building code.

Example fix

// before (conceptual)
req.body = "{\"ok\":true}" // string
req.bodyType = rawBody
// Send() -> ErrBodyType

// after
req.Body([]byte(`{"ok":true}`))
Defensive patterns

Strategy: type-guard

Validate before calling

func mustBeByteSlice(body any) error {
    if _, ok := body.([]byte); !ok { return errors.New("raw body must be []byte") }
    return nil
}

Type guard

func isByteSlice(v any) bool { _, ok := v.([]byte); return ok }

Try / catch

if err := req.Send(); err != nil {
    if errors.Is(err, fiber.ErrBodyType) {
        // convert body to []byte and retry
    }
}

Prevention

When it happens

Trigger: Calling the client's raw-body setter with a non-[]byte value, or internal code setting bodyType=rawBody while assigning req.body = someString. The public Body()/RawBody() helpers normally force []byte; the error surfaces when the bodyType/body pair is set inconsistently (e.g. via reflection or a future setter).

Common situations: Mistakenly assigning a string to the body while leaving the body type as raw; building a Request through code paths that don't go through the typed setters; serializing data yourself but passing the wrong variable.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/227be73e696c0dc8. Report an issue: GitHub.

Appendix: source

Thrown at client/core.go:304

// It's caller's responsibility to ensure that:
// - the channel is not closed
// - the channel is drained before returning it
// - the channel is not reused after returning it
func releaseErrChan(ch chan error) {
	errChanPool.Put(ch)
}

// newCore returns a new core object.
func newCore() *core {
	return &core{}
}

var (
	ErrTimeoutOrCancel      = errors.New("timeout or cancel")
	ErrURLFormat            = errors.New("the URL is incorrect")
	ErrNotSupportSchema     = errors.New("protocol not supported; only http or https are allowed")
	ErrFileNoName           = errors.New("the file should have a name")
	ErrBodyType             = errors.New("the body type should be []byte")
	ErrNotSupportSaveMethod = errors.New("only file paths and io.Writer are supported")
	ErrBodyTypeNotSupported = errors.New("the body type is not supported")
)

View on GitHub (pinned to a105acad6c)