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 text codec reads the whole connection as raw bytes and can only decode into *string, *[]byte, or *text.Frame. ReadBody returns this error for any other pointer type. Despite the message text, *string and *Frame are also accepted — the message only lists *[]byte.

Source

Thrown at codec/text/text.go:39

	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 *string:
		*v = string(buf)
	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 ve := b.(type) {
	case *Frame:
		v = ve.Data
	case *[]byte:
		v = *ve
	case *string:
		v = []byte(*ve)
	case string:
		v = []byte(ve)
	case []byte:
		v = ve

View on GitHub (pinned to 24529f1404)

Solutions

  1. Pass *[]byte, *string, or *text.Frame as the body target
  2. In handlers, accept a *text.Frame (or *[]byte) and unmarshal the payload yourself
  3. Switch the service back to a structured codec (json/proto) if you need typed request/response objects
  4. Set the request/response content type explicitly so the text codec is only selected for raw payloads

Example fix

// before
var req MyStruct
codec.ReadBody(&req) // failed to read body
// after
var frame text.Frame
if err := codec.ReadBody(&frame); err != nil { return err }
json.Unmarshal(frame.Data, &req)
Defensive patterns

Strategy: type-guard

Validate before calling

func bodyTargetOk(b interface{}) bool {
    switch b.(type) {
    case *string, *[]byte, *Frame:
        return true
    }
    return false
}

Type guard

func isTextBodyTarget(b interface{}) bool {
    switch b.(type) {
    case *string, *[]byte, *text.Frame:
        return true
    default:
        return false
    }
}

Prevention

When it happens

Trigger: Calling text codec ReadBody with a target like *map[string]string, a struct pointer, or nil; usually reached by using the text codec with a service whose handler signature expects a typed request/response struct.

Common situations: Publishing/subscribing raw text payloads while handlers decode into typed structs; switching a service's codec to text without changing handler signatures; using client.Call with non-byte response types under content-type text/*.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/f99b4bbab57d4d90. Report an issue: GitHub.