CoAP Packet Structure

The following diagram shows the structure of a CoAP packet


Version (2 bits) - CoAP version. Always 01 T (2 bits) - Transaction type TKL (4 bits) - Token length Code (8 bits) - Request or response code

Request codes

Binary

Code

Type

GET

1

00000001

POST

2

00000010

PUT

3

00000011

DELETE

4

00000100

Response codes

Type

Code

Binary

Created

2.01


Deleted

2.02


Changed

2.04


Content

2.05


Bad Request

4.00


Unauthorized

4.01


Not found (resource)

4.04


Method not allowed

4.05


Message ID (16 bits) - Unique identifier for the packet. Used for duplicate detection. Token (up to 64 bits) - Used to match a response with a request Options (x bits) - Both requests and responses may include a list of one or more options. Payload Marker (8 bits) - Marks the beginning of the payload. Always 11111111

Example for publishing an event

For this, we are going to show the packet structure of a device publishing an Event to Wia. The packet itself, along with the payload, should be transmitted in hex format.



This shows a POST request (Code: 02) being submitted to the path "events" with the payload structure {"accessToken":"d_sk_abc123","name":"hello-wia","data":"12345"}.

To test with your own device, make a request replacing d_sk_abc123 with your device's access token.