CoAP Content-Format Mismatch Causes Parse Error
DodaTech
Updated 2026-06-26
1 min read
In this tutorial, you'll learn about CoAP Content. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
CoAP client receives content in unexpected format, causing parse failure.
Quick Fix
Wrong
res.end(jsonData);
// No Content-Format option set```
Client receives binary data but cannot determine format. Parse fails.
### Right
```cpp
#include <coap.h>
void get_handler(coap_context_t *ctx, coap_resource_t *res,
coap_session_t *session, coap_pdu_t *request,
coap_binary_t *token, coap_string_t *query,
coap_pdu_t *response) {
unsigned char buf[] = "{"temp":25.5,"unit":"celsius"}";
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
// Set correct Content-Format for JSON
coap_set_header_content_type(response, COAP_MEDIATYPE_APPLICATION_JSON);
// Client can now parse correctly based on Content-Format
coap_add_data(response, sizeof(buf) - 1, buf);
}```
Client receives 2.05 with Content-Format: 50 (application/json). Parse succeeds.
## Prevention
CoAP Content-Format option identifies the media type of the payload. Common values: 0=text/plain, 40=application/link-format, 41=application/xml, 42=application/octet-stream, 47=application/exi, 50=application/json, 60=application/cbor. Always set Content-Format before sending the response. The client uses this to parse the payload correctly.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
## FAQ
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### Most common Content-Format values?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>0=text/plain, 40=link-format, 42=octet-stream, 47=EXI, 50=JSON, 60=CBOR, 61-64=signed/encrypted JSON and CBOR.</p>
<h3 id="what-if-content-format-is-missing">What if Content-Format is missing?</h3><p>The client should assume text/plain (0) or reject the response. Best practice: always set Content-Format explicitly.</p>
<h3 id="can-i-accept-multiple-formats">Can I accept multiple formats?</h3><p>Yes. Use accept option in request (similar to HTTP Accept header). Server returns the best matching format.</p>
</div></details>
← Previous
CoAP Block-Wise Transfer Fails — Complete Guide
Next →
CoAP DELETE Request Returns 4.05 Method Not Allowed
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro