{"record":{"id":"227be73e696c0dc8","repo":"gofiber/fiber","slug":"the-body-type-should-be-byte","errorCode":null,"errorMessage":"the body type should be []byte","messagePattern":"the body type should be \\[\\]byte","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/core.go","lineNumber":304,"sourceCode":"// It's caller's responsibility to ensure that:\n// - the channel is not closed\n// - the channel is drained before returning it\n// - the channel is not reused after returning it\nfunc releaseErrChan(ch chan error) {\n\terrChanPool.Put(ch)\n}\n\n// newCore returns a new core object.\nfunc newCore() *core {\n\treturn &core{}\n}\n\nvar (\n\tErrTimeoutOrCancel      = errors.New(\"timeout or cancel\")\n\tErrURLFormat            = errors.New(\"the URL is incorrect\")\n\tErrNotSupportSchema     = errors.New(\"protocol not supported; only http or https are allowed\")\n\tErrFileNoName           = errors.New(\"the file should have a name\")\n\tErrBodyType             = errors.New(\"the body type should be []byte\")\n\tErrNotSupportSaveMethod = errors.New(\"only file paths and io.Writer are supported\")\n\tErrBodyTypeNotSupported = errors.New(\"the body type is not supported\")\n)\n","sourceCodeStart":286,"sourceCodeEnd":308,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/client/core.go#L286-L308","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Always pass []byte to the raw body setter: req.Body([]byte(\"payload\")).","If you have a string, convert first: req.Body([]byte(s)).","Use the typed setters (JSON, XML, Form) for structured payloads — they handle serialization and set the correct bodyType.","Add a unit test that asserts req.body has type []byte after your custom request-building code."],"exampleFix":"// before (conceptual)\nreq.body = \"{\\\"ok\\\":true}\" // string\nreq.bodyType = rawBody\n// Send() -> ErrBodyType\n\n// after\nreq.Body([]byte(`{\"ok\":true}`))","handlingStrategy":"type-guard","validationCode":"func mustBeByteSlice(body any) error {\n    if _, ok := body.([]byte); !ok { return errors.New(\"raw body must be []byte\") }\n    return nil\n}","typeGuard":"func isByteSlice(v any) bool { _, ok := v.([]byte); return ok }","tryCatchPattern":"if err := req.Send(); err != nil {\n    if errors.Is(err, fiber.ErrBodyType) {\n        // convert body to []byte and retry\n    }\n}","preventionTips":["Use the typed setters (req.JSON, req.XML, req.Form) for structured payloads.","Convert strings with []byte(s) before assigning to a raw body.","Add a test asserting req.body has type []byte after custom request-building code."],"tags":["client","request-body","types","http"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}