← Back to the chapter · Previous detail

Technical detail · CoAP Messages: Codes, Tokens, and Options

Extended option encoding and complete message size

CoAP over UDP encodes options as numbered message fields whose headers give an option-number increase (delta) and a value length.

Each delta and length starts in a nibble, a four-bit field. For either delta or length, nibble values 0–12 encode directly. Value 13 requires one extra byte plus 13; value 14 requires two extra bytes interpreted as an unsigned integer plus 269. Value 15 is reserved. Read the delta extension first, then the length extension, then the value bytes. At the datagram end, stop; a message without content needs no payload marker.

3.13.4 Worked Example: Complete Sensor Reading – CoAP vs HTTP

3.14 Putting Numbers to It

For a GET request for /sensors/temp with a four-byte fixed header, a four-byte Token for response matching, and two Uri-Path options, each with a one-byte header and values of seven and four bytes, the request totals 4 + 4 + (1 + 7) + (1 + 4) = 21 CoAP bytes. For a response with a four-byte header, a four-byte Token, a one-byte Content-Format option header and one-byte value, a one-byte payload marker and a 24-byte JSON payload, the response totals 4 + 4 + (1 + 1) + 1 + 24 = 35: header, Token, Content-Format, marker and JSON payload. These counts exclude transport, IP, security, link framing and retransmissions. They establish message size, not battery lifetime.

Accept, option 17, expresses the requested response format; Content-Format identifies the format actually carried. JSON encodes structured values as text. CBOR, Concise Binary Object Representation, encodes them in binary and uses Content-Format 60. Choose a format both endpoints implement and measure complete serialized bytes before claiming a saving. The format changes decoding, not which request the Token matches.

← Back to the chapter · Previous detail