{"record":{"id":"855e95deefc88cc5","repo":"micro/go-micro","slug":"failed-to-read-body-v-is-not-type-of-byte","errorCode":null,"errorMessage":"failed to read body: %v is not type of *[]byte","messagePattern":"failed to read body: (.+?) is not type of \\*\\[\\]byte","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codec/bytes/bytes.go","lineNumber":37,"sourceCode":"\nfunc (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error {\n\treturn nil\n}\n\nfunc (c *Codec) ReadBody(b interface{}) error {\n\t// read bytes\n\tbuf, err := io.ReadAll(c.Conn)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tswitch v := b.(type) {\n\tcase *[]byte:\n\t\t*v = buf\n\tcase *Frame:\n\t\tv.Data = buf\n\tdefault:\n\t\treturn fmt.Errorf(\"failed to read body: %v is not type of *[]byte\", b)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Codec) Write(m *codec.Message, b interface{}) error {\n\tvar v []byte\n\tswitch vb := b.(type) {\n\tcase *Frame:\n\t\tv = vb.Data\n\tcase *[]byte:\n\t\tv = *vb\n\tcase []byte:\n\t\tv = vb\n\tdefault:\n\t\treturn fmt.Errorf(\"failed to write: %v is not type of *[]byte or []byte\", b)\n\t}\n\t_, err := c.Conn.Write(v)","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/codec/bytes/bytes.go#L19-L55","documentation":"The bytes codec's Read method decodes a raw byte buffer into the destination body, which must be *[]byte or *Frame. If the caller passes any other pointer type (or nil of an unsupported type), the type switch falls through to default and this error is returned, leaving the destination untouched.","triggerScenarios":"Calling codec.Read/ReadBody on a bytes.Codec with a body argument other than *[]byte or *Frame, e.g. *string, *map[string]interface{}, or a struct pointer.","commonSituations":"Users swapping codecs (e.g. from json codec to bytes codec) without changing the message body type; generic helper code that passes interface{} bodies assuming JSON unmarshaling; wrong codec registered on client/server options.","solutions":["Pass a *[]byte as the body destination.","Or pass *Frame if you need frame metadata (v.Data).","Ensure the bytes codec is only used where the body contract is raw bytes; use the json codec for structured types.","Check which codec your client/server was constructed with (micro client.Codec options)."],"exampleFix":"// before\nvar out map[string]interface{}\nc.Read(m, &out)\n// after\nvar out []byte\nc.Read(m, &out)","handlingStrategy":"type-guard","validationCode":"func isBytesBody(b interface{}) bool {\n\tswitch b.(type) {\n\tcase *[]byte, *Frame:\n\t\treturn true\n\t}\n\treturn false\n}","typeGuard":"func asBytesBody(b interface{}) (*[]byte, bool) {\n\tp, ok := b.(*[]byte)\n\treturn p, ok\n}","tryCatchPattern":"var out []byte\nif err := c.Read(m, &out); err != nil {\n\tif strings.Contains(err.Error(), \"is not type of\") {\n\t\t// wrong codec or body type; reconfigure or decode differently\n\t}\n\treturn err\n}","preventionTips":["Match body types to the codec: bytes codec -> *[]byte/*Frame.","Do not reuse JSON-shaped body structs with the bytes codec.","Check the codec registered in client/server options when switching transports.","Add a unit test for each codec+body combination you use."],"tags":["codec","type-mismatch","bytes"],"backgroundTag":"codec-body-type-mismatch","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}