{"record":{"id":"f99b4bbab57d4d90","repo":"micro/go-micro","slug":"failed-to-read-body-v-is-not-type-of-byte-f99b4b","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/text/text.go","lineNumber":39,"sourceCode":"\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 *string:\n\t\t*v = string(buf)\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 ve := b.(type) {\n\tcase *Frame:\n\t\tv = ve.Data\n\tcase *[]byte:\n\t\tv = *ve\n\tcase *string:\n\t\tv = []byte(*ve)\n\tcase string:\n\t\tv = []byte(ve)\n\tcase []byte:\n\t\tv = ve","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/codec/text/text.go#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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/*.","solutions":["Pass *[]byte, *string, or *text.Frame as the body target","In handlers, accept a *text.Frame (or *[]byte) and unmarshal the payload yourself","Switch the service back to a structured codec (json/proto) if you need typed request/response objects","Set the request/response content type explicitly so the text codec is only selected for raw payloads"],"exampleFix":"// before\nvar req MyStruct\ncodec.ReadBody(&req) // failed to read body\n// after\nvar frame text.Frame\nif err := codec.ReadBody(&frame); err != nil { return err }\njson.Unmarshal(frame.Data, &req)","handlingStrategy":"type-guard","validationCode":"func bodyTargetOk(b interface{}) bool {\n    switch b.(type) {\n    case *string, *[]byte, *Frame:\n        return true\n    }\n    return false\n}","typeGuard":"func isTextBodyTarget(b interface{}) bool {\n    switch b.(type) {\n    case *string, *[]byte, *text.Frame:\n        return true\n    default:\n        return false\n    }\n}","tryCatchPattern":null,"preventionTips":["With the text codec, decode into *text.Frame or *[]byte and unmarshal yourself","Do not use text content-type for services with typed request/response structs","Set content types explicitly instead of relying on negotiation defaults"],"tags":["codec","text","type-mismatch","go"],"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"}