{"record":{"id":"828c2d1c7f92fa08","repo":"micro/go-micro","slug":"failed-to-write-v-is-not-type-of-byte-or-by","errorCode":null,"errorMessage":"failed to write: %v is not type of *[]byte or []byte","messagePattern":"failed to write: (.+?) is not type of \\*\\[\\]byte or \\[\\]byte","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codec/bytes/bytes.go","lineNumber":53,"sourceCode":"\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)\n\treturn err\n}\n\nfunc (c *Codec) Close() error {\n\treturn c.Conn.Close()\n}\n\nfunc (c *Codec) String() string {\n\treturn \"bytes\"\n}\n\nfunc NewCodec(c io.ReadWriteCloser) codec.Codec {\n\treturn &Codec{\n\t\tConn: c,\n\t}\n}","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/codec/bytes/bytes.go#L35-L71","documentation":"The bytes codec's Write method only accepts bodies of type *Frame, *[]byte, or []byte, since it writes raw bytes straight to the connection. Any other type fails with this error before any network write occurs, so the connection is left unmodified.","triggerScenarios":"Calling codec.Write (via serveReq or client send path) on a bytes.Codec with a body that is not []byte, *[]byte, or *Frame, e.g. a string or struct.","commonSituations":"Publishing/requesting with a payload that was built for the JSON codec; handlers marshaling to string instead of []byte; framework misconfiguration where bytes codec is registered but request/response bodies are structured.","solutions":["Marshal your payload to []byte before writing (json.Marshal or []byte(str)).","Pass *Frame or *[]byte if the caller provides a pointer.","Register the correct codec (jsonrpc/grpc) for structured messages.","Inspect the call site (e.g. serveReq) to see what body type it builds and align it with the codec."],"exampleFix":"// before\nc.Write(m, \"hello\")\n// after\nc.Write(m, []byte(\"hello\"))","handlingStrategy":"type-guard","validationCode":"func isWritableBytes(b interface{}) bool {\n\tswitch b.(type) {\n\tcase []byte, *[]byte, *Frame:\n\t\treturn true\n\t}\n\treturn false\n}","typeGuard":"func toBytes(b interface{}) ([]byte, bool) {\n\tswitch v := b.(type) {\n\tcase []byte:\n\t\treturn v, true\n\tcase *[]byte:\n\t\treturn *v, true\n\tcase *Frame:\n\t\treturn v.Data, true\n\t}\n\treturn nil, false\n}","tryCatchPattern":"body, ok := toBytes(payload)\nif !ok {\n\tbody, _ = json.Marshal(payload) // marshal structured payloads first\n}\nif err := c.Write(m, body); err != nil { return err }","preventionTips":["Marshal strings/structs to []byte before writing with the bytes codec.","Confirm the codec type before assuming structured writes work.","Keep a helper that normalizes payloads to []byte at the transport boundary.","Cover the write path with a codec-specific integration test."],"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"}