Skip to content

Choose binary or structured mode

Both are envelope.Formats, so the choice is the one value you give a writer.

Prefer binary

Binary mode puts each attribute in a header, so anything routing on an attribute can see it without parsing the body. Pick the binding for the transport:

var writer envelope.Format = cloudevents.Binary(cloudevents.NATS) // or HTTP, Kafka, AMQP

Use structured when the transport limits its headers

SQS carries ten message attributes, and an event with every optional attribute set and three extensions needs eleven in binary mode. SNS also refuses a body that is not UTF-8, which rules out a protobuf payload. Structured mode carries both, because the whole event is the body:

writer = cloudevents.Structured()

Publish

Encode returns an envelope.Wire. Put ContentType wherever the transport keeps its content type: a native property on AMQP and RabbitMQ, the content-type header on NATS and SQS. The NATS form is in Send a CloudEvent over NATS.

Change a writer without a flag day

A reader that accepts both modes can be deployed before any writer changes. See Read more than one format.

Do not set a binary event's content type to a CloudEvents one

Binary(b).Encode refuses an event whose datacontenttype starts application/cloudevents with ErrReadsAsStructured. Publish it with Structured(), or give it a content type such as application/json.