Chapters

Parsing and Validation: CoAP Messages Reference

Return to the parent chapter.

Related reference: CoAP message types.

Start With the Decision

A sensor receives a request for /sensors. Before accepting a parser’s output, locate the Token and the first option yourself. A wrong Token length moves the option boundary and changes every field that follows.

CoAP means Constrained Application Protocol. This page uses its base message format over User Datagram Protocol (UDP), which carries separate messages called datagrams. A byte has eight bits; hexadecimal writes a byte with two digits. A packet trace records messages observed on a link.

Predict before reading the worked decode. The fixed header is four bytes; the low four bits of the first byte give TKL. In the unseparated request below, identify the Token bytes, the first option boundary, and the path. Write your prediction, then compare it with the explanation.

42 01 7A 10 A1 B2 B7 73 65 6E 73 6F 72 73

The first byte, 42, splits into 01 | 00 | 0010: version 1, Confirmable type, and Token Length (TKL) 2. Confirmable (CON) asks the receiver for an acknowledgment. The next byte, 01, is Code 0.01, the GET method, meaning read. Code is written as class.detail: requests use class 0; response classes 2, 4, and 5 indicate success, client error, and server error. The final two header bytes hold Message ID 0x7A10, used for acknowledgment matching and duplicate detection.

Consume the next two bytes, A1 B2, as the Token: the client-selected value used to match a response to its request. Only then read B7 as an option header. Each half of this header is four bits. An option is a numbered metadata field. Here its two halves mean delta 11 and length 7: the first option number is 0 + 11 = 11, called Uri-Path, and its seven value bytes spell sensors. The path /sensors names the resource being read. All 14 bytes are accounted for; this request has no payload, the message’s application content.

Your task is to reconstruct the path, check each boundary, and distinguish message acknowledgment from response correlation. The same rules should work when another path segment or a delayed response appears.

Route Overview

This is part 3 of 3. Review CoAP Messages: Codes, Tokens, and Options for the preceding evidence.

Learning Objectives

  • Locate the header, Token, options, and optional payload in a base-format CoAP/UDP message.
  • Decide whether option lengths and request-response identifiers agree with the bytes, and explain the decision.

Chapter Roadmap

  • Label the Diagram
  • CoAP Fundamentals
  • Summary
  • How It Works: Option Delta Encoding Saves Bytes
  • Concept Relationships
  • See Also
  • What’s Next
Label the Diagram

Knowledge check · self-test

Knowledge check

Test your understanding.

CoAP Fundamentals

Start With a Sleeping Sensor

Follow One Request to a Physical Result

A sleeping sensor wakes and receives the worked GET /sensors. Its parser first needs a valid field layout. Whether the request can wake the device, whether the sender has permission, and whether a later valve command succeeds require separate evidence.

A resource is a named item a device exposes, such as a sensor reading. Its URI path identifies it; a method says what to do with it. A GET reads a resource’s representation, the returned data describing that resource. Hypertext Transfer Protocol (HTTP) uses the same resource-and-method vocabulary. This page checks how CoAP encodes that vocabulary.

Read the leak state, repeat the request, delay the reply, send a wrong command, and restart the gateway. The valve should reach one named safe state. The receiver should distinguish no reply, refusal, and completed action.

That procedure belongs to a later system test. A gateway bridges networks; an actuator produces a physical action, such as moving a valve. A correctly decoded request or an acknowledgment alone does not prove that action occurred.

Overview: CoAP Makes Resources Cheap Enough for Constrained Nodes

In the worked request, 01 says GET and the Uri-Path option says sensors. Together they mean read /sensors. The four-byte header frames the message; the Token connects the eventual response to this request.

A client sends requests; a server handles them. The sensor is the server here. It may return a representation of /sensors with a response Code. The method belongs in the Code byte, while the resource path belongs in options.

Message Type controls delivery handling. CON requests an acknowledgment (ACK); Non-confirmable (NON) has no message-layer acknowledgment. Reset (RST) indicates that the recipient could not process the message in its context. A NON request can still receive an application response. Type alone does not tell you whether a requested physical action succeeded.

Read the message in order: fixed header, the number of Token bytes declared by TKL, then options and any payload. Use Message ID for acknowledgment and duplicate handling, and Token for response correlation with the peer.

In the figure in the parent chapter, compare CON and NON handling, then locate Type and Message ID in the fixed header. These are the fields used to interpret acknowledgment behavior for the worked request.

The CON branch in the figure in the parent chapter shows an ACK carrying a response. A delayed response can instead arrive after an empty ACK, as the later flow figure illustrates. The NON branch has no message acknowledgment; that does not prevent a response to a NON request. For the running example, keep Message ID 0x7A10 separate from Token A1 B2 even when an ACK also carries the result.

Resource model

For GET /sensors, verify both the method in Code and the resource path in Uri-Path. Neither field supplies the other.

Compact messages

The worked message uses four header bytes, two Token bytes, and eight option bytes. Its total is 14 CoAP bytes, excluding UDP and other network headers.

UDP transport

UDP carries the datagram. CoAP Type and Message ID support message handling; they do not authenticate the sender or validate the returned measurement.

Protocol fit

First establish that this resource exchange fits the device and network. Then check the actual message boundaries rather than choosing from a protocol name alone.

Practitioner: Write the CoAP Exchange Record

Predict the new path and byte count before extending the running request: append 04 74 65 6D 70. Compare your prediction: the header 04 means delta 0 and length 4, so the option number stays 11 and the value spells temp. The repeated Uri-Path adds a second segment. The request now has 19 CoAP bytes.

Write the extended exchange beside the decoded bytes: GET, /sensors/temp, CON, Message ID 0x7A10, and Token A1 B2. Add the expected response Code and representation format so a reviewer can compare intent with the trace.

A matching response Token identifies the request within the exchange with that peer. It does not prove that the data is fresh, that the sender is authorized, or that a physical reading is correct. Keep those acceptance conditions in the record.

Field
Example
Review Question
Evidence
Resource
/sensors, then /sensors/temp
Does the URI name one stable resource rather than an implementation detail?
Decoded Uri-Path segments agree with the recorded resource path.
Method
GET in Code 0.01
Does the Code describe the intended read?
Request Code is GET; response Code and representation match the read contract.
Reliability
CON for this worked GET
What should happen if the ACK or response is missing?
Loss test shows acceptable retry, latency, or drop behavior.
Caching
Max-Age: the response option defining its freshness lifetime
For how long may a client reuse this response as fresh?
Freshness test matches the published Max-Age value.

Practitioner Checklist

Compare the record with the trace in order. Check method and path, then header and Token boundaries, then each option length. Check ACK or RST Message ID against the message it answers; check a response Token against the outstanding request to that peer. Record representation format and freshness, and obtain separate evidence for server identity, operation permission, and any physical effect.

Under the Hood: Message Layer, Tokens, and Failure Boundaries

The worked CON uses Message ID 0x7A10 and Token A1 B2. An empty ACK can acknowledge that message by repeating its Message ID without carrying the application response. A later, separate response carries the request Token and uses a new message exchange.

To diagnose a delayed result, ask two questions: was this CON acknowledged, and which outstanding request does the response answer? The Message ID answers the first question; the Token, together with the peer, answers the second. An ACK is evidence of message handling, not proof of a completed valve action.

Use Figure 1 to compare an immediate response with a delayed, separate response.

Compare CON, NON, and separate-response flows between client and server; the empty ACK precedes the delayed result, separating message acknowledgment from response correlation.
Compare the immediate and separate responses: an empty ACK acknowledges a message, while the later response uses the request Token for correlation.

In Figure 1, distinguish the empty acknowledgment from the later response. Match the former by Message ID and the latter by Token within the peer exchange. Then inspect the actual option and payload boundaries in a trace; a flow diagram alone cannot validate those bytes.

Message ID

Match an ACK or RST to the message it answers and detect duplicates. For the running CON, the acknowledgment Message ID is 0x7A10.

Token

Match a response to the request at that peer. A separate response to the running request carries A1 B2, even though it has its own Message ID.

Options

Uri-Path carries sensors in the running request. Options can also describe representation format and caching. The method remains in Code.

Failure boundary

UDP can lose, reorder, or duplicate datagrams. CoAP handles selected cases, but the application still defines acceptable consequences.

Common Boundary Decisions

Choose CON or NON from the consequences of loss and the retry policy. For the worked GET, record whether a missing ACK or a missing response caused the timeout. If the response arrives, check the Token before accepting its data. Security, freshness, and physical effects remain separate review questions; changing the message type cannot establish them.

Simulation · interactive

CoAP Discovery Workbench

Explore how the system behaves.

Summary

Reconstruct the worked request from its fields: CON GET, Message ID 0x7A10, Token A1 B2, and path /sensors. The bytes establish this encoding; they do not establish permission, freshness, or a physical outcome.

For a delayed reply, separate the acknowledgment from the application response. That distinction prevents a matching Message ID from being mistaken for a completed request.

Key Takeaway

Validate boundaries first, correlate the response with the Token and peer, then check the application result against the exchange record.

See Also

Decode this unseen complete datagram: 41 01 7A 11 C3 B3 61 69 72 02 72 68. ASCII 61 69 72 spells air; 72 68 spells rh. Which exchange record matches?

CON GET /air/rh, MID 0x7A11, Token C3; 12 bytes with no payload.

41 gives TKL 1. B3 declares option 11, length 3; 02 adds delta 0, length 2. The path is /air/rh. Total: 4 + 1 + 1 + 3 + 1 + 2 = 12 bytes.

A client sends CON GET /air/rh to peer S, MID 0x7A11, Token C3. S returns empty ACK MID 0x7A11, then CON 2.05 MID 0x9100, Token D4. No other requests are outstanding. What follows, and what remains unproved?

The request was acknowledged, but the response does not correlate. Investigate the Token mismatch; freshness and physical correctness remain unproved.

The ACK matches MID 0x7A11. D4 does not match request Token C3 at peer S. Even a matching Token would not establish authorization, freshness, or physical state.

Summary

For the running GET /sensors, account for every byte:

  • The four-byte header includes version, Type, TKL, Code, and Message ID.
  • TKL 2 places Token A1 B2 immediately after the header; base-format Tokens can be 0–8 bytes long.
  • B7 declares option delta 11 and value length 7. The value spells sensors.
  • The total is 4 + 2 + 8 = 14 CoAP bytes, excluding transport and link overhead.

An ACK matches the CON by Message ID. A response matches its request by Token within the peer exchange. A syntactically consistent message still needs authorization, representation validation, and freshness checks.

How It Works: Option Delta Encoding Saves Bytes

Decode a second unseen message before consulting feedback:

41 01 7A 11 C3 B3 61 69 72 02 72 68

Mark the header and Token, accumulate option numbers, reconstruct the path, and count the bytes. ASCII 61 69 72 spells air; 72 68 spells rh. Explain whether a two-byte Token interpretation agrees with TKL. Use the exchange-record check above to submit your prediction and reveal the worked feedback.

Check a changed boundary. Suppose the complete datagram instead ends with B7 followed by only six value bytes. Decide whether that option is complete and whether this evidence says anything about response correlation. Use the exchange-record check’s changed-case feedback to compare your reasoning.

An option delta is the difference from the previous option number, starting at zero. Its length tells you how many bytes to consume for the value. Advance over the whole value before reading another option header. If a payload is present, its 0xFF marker is recognized at an option boundary; a byte inside an option value is still part of that value.

Compare the encoding costs for the worked /sensors/temp extension:

Hypothetical format with separate one-byte number and length:
  sensors: 1 + 1 + 7 = 9 bytes
  temp:    1 + 1 + 4 = 6 bytes
  total: 15 bytes

CoAP headers for these short options pack delta and length:
  sensors: 1 + 7 = 8 bytes
  temp:    1 + 4 = 5 bytes
  total: 13 bytes

The two-byte saving is against that hypothetical format. It comes from packing delta and length into one byte in these examples; it is not a universal saving against HTTP or every possible absolute-number encoding.

For /sensors/temp/reading/current, the four short segments need four one-byte option headers, rather than eight header bytes in the hypothetical format. For /api/v2/devices/sensor42/temperature/readings/latest, the seven short segments need seven rather than fourteen. Both examples start with delta 11 and use delta 0 for the remaining Uri-Path options. Larger deltas or lengths need the extended encoding covered in the wire-format page.

Why sorting matters:

Options appear in nondecreasing option-number order:

  • Uri-Path (11), Uri-Path (11), Content-Format (12) gives deltas 11, 0, 1.
  • Content-Format (12), Uri-Path (11) would require delta -1, which cannot be encoded.

Content-Format identifies the payload representation format. Keep repeated Uri-Path segments in their path order while sorting by option number. The general rule is to accumulate option numbers and consume declared lengths, rather than guessing field boundaries from readable text.

Maths · guided foundations

Bytes, Airtime, and the Duty-Cycle Tax

Check the quantities and assumptions.

Concept Relationships

This chapter connects binary protocol details to practical implementation:

Foundation Knowledge:

Review why CoAP uses a compact message, then revisit binary representation to decode its fields.

Message Structure:

  • 4-byte fixed header (version, type, token length, code, message ID)
  • Variable-length token (0-8 bytes) for request-response matching
  • Delta-encoded options (metadata like Uri-Path, Content-Format)
  • Optional payload carrying application data, following the payload marker

Related Protocols:

Compare the CoAP fields with other protocol encodings. Check the HTTP version before assuming its headers are text.

  • HTTP Headers - Compare HTTP version-specific header encodings
  • MQTT Message Format - Alternative binary protocol
  • Protocol Buffers - Similar binary encoding philosophy

Implementation:

Use the implementation lab to build messages, then inspect them in Wireshark to check the encoded fields.

See Also

Specifications:

Read the message format rules first. Then look up each option number in the registry.

Tools:

Use the Wireshark reference to inspect captured fields, then try a CoAP client to produce traffic.

Binary Encoding:

Concise Binary Object Representation (CBOR) encodes application data in a payload. Review it separately from the option headers that locate and describe that payload.

Debugging:

Check common response errors first, then trace token matching when a response cannot be paired with its request.

What’s Next

Use these chapters to investigate the questions left after a boundary and correlation check:

ChapterFocusWhy Read It
CoAP Message TypesCON, NON, ACK, RST reliability mechanicsUnderstand exactly how retransmission timers, exponential backoff, and duplicate detection operate at the message level
CoAP Observe ExtensionServer-push subscription modelStudy how an Observe registration lets a server send later resource-change notifications
CoAP Block-Wise TransfersFragmentation of large payloadsLearn the Block1/Block2 options that split firmware images and large datasets across multiple CoAP messages
CoAP Implementation LabsHands-on Python/ESP32 codingApply the binary format knowledge by sending and receiving real CoAP messages with aiocoap and MicroPython
CoAP Security with DTLSEncryption and authenticationSecure the tokens and payloads you now understand at the binary level using DTLS record layer wrapping
CoAP Fundamentals and ArchitectureResource and exchange overviewRevisit the resource model and the evidence needed to review an exchange

Continue Your Route

This is the last part of this chapter. Return to CoAP Messages: Codes, Tokens, and Options or continue from the coap module index.

Hi, I’m Broker Bex. What do you need?

Find help

Let’s find what you need

Find a lesson or return to your module guide.

Give feedback

Help improve this chapter

Tell us what helped or what needs fixing.

Tips

Try a small next step

A few ways to get more from this chapter.

Ask AI

Continue the conversation

Take the conversation further with your preferred assistant.

AI help is available in supported modules when enabled in Settings.

No live person answers on independent study. Feedback does not promise a personal reply.

For welfare or safety concerns, follow your school’s procedures outside IoTClass.