micro/go-micro · error
failed to read body: %v is not type of *[]byte
Error message
failed to read body: %v is not type of *[]byte
What it means
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.
Source
Thrown at codec/bytes/bytes.go:37
func (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *Codec) ReadBody(b interface{}) error {
// read bytes
buf, err := io.ReadAll(c.Conn)
if err != nil {
return err
}
switch v := b.(type) {
case *[]byte:
*v = buf
case *Frame:
v.Data = buf
default:
return fmt.Errorf("failed to read body: %v is not type of *[]byte", b)
}
return nil
}
func (c *Codec) Write(m *codec.Message, b interface{}) error {
var v []byte
switch vb := b.(type) {
case *Frame:
v = vb.Data
case *[]byte:
v = *vb
case []byte:
v = vb
default:
return fmt.Errorf("failed to write: %v is not type of *[]byte or []byte", b)
}
_, err := c.Conn.Write(v)View on GitHub (pinned to 24529f1404)
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).
Example fix
// before
var out map[string]interface{}
c.Read(m, &out)
// after
var out []byte
c.Read(m, &out) Defensive patterns
Strategy: type-guard
Validate before calling
func isBytesBody(b interface{}) bool {
switch b.(type) {
case *[]byte, *Frame:
return true
}
return false
} Type guard
func asBytesBody(b interface{}) (*[]byte, bool) {
p, ok := b.(*[]byte)
return p, ok
} Try / catch
var out []byte
if err := c.Read(m, &out); err != nil {
if strings.Contains(err.Error(), "is not type of") {
// wrong codec or body type; reconfigure or decode differently
}
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- failed to write: %v is not type of *[]byte or []byte
- failed to marshal: %v is not type of *[]byte
- failed to unmarshal: %v is not type of *[]byte
- failed to read body: %v is not type of *[]byte
- failed to write: %v is not type of *[]byte or []byte
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/855e95deefc88cc5.
Report an issue: GitHub.