Skip to content

CoAP Block-Wise Transfer Fails for Large Payloads

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about CoAP Block. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Large CoAP payloads fail to transfer completely using Block-wise transfer.

Quick Fix

Wrong

res.end(largePayload);  // Exceeds UDP datagram size (~64 KB)```

Response truncated at UDP datagram boundary. Payload incomplete.


### Right

```cpp
#include <coap.h>

#define BLOCK_SIZE 1024

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) {
  // Large payload (e.g., firmware binary)
  static const unsigned char firmware[] = "FIRMWARE_V2.1...";
  size_t fw_len = sizeof(firmware) - 1;

  // Set Block2 option for block-wise transfer
  coap_add_data_blocked_response(response, __FILE__, __LINE__,
                                 firmware, fw_len,
                                 COAP_MEDIATYPE_APPLICATION_OCTET_STREAM,
                                 6);  // SZX=6 → 1024 byte blocks
}```

Large payload transferred in 1024-byte blocks. Client reassembles.


## Prevention

CoAP Block-wise transfer (RFC 7959) splits large payloads into 2^SZX blocks (16-1024 bytes). Block1 for request body (POST/PUT), Block2 for response (GET). The MORE flag indicates more blocks. SZX 0=16, 1=32, 2=64, 3=128, 4=256, 5=512, 6=1024 bytes. MAX_BLOCK_SZX is typically 6.

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">### What is SZX?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Block size exponent. SZX 0=16B, 1=32B, 2=64B, 3=128B, 4=256B, 5=512B, 6=1024B.</p>
<h3 id="how-does-client-know-more-blocks">How does client know more blocks?</h3><p>Block2 option has MORE (M) bit. M=1 = more blocks follow. Client increments NUM field and requests next block.</p>
<h3 id="block1-vs-block2">Block1 vs Block2?</h3><p>Block1 controls request body transfer (POST/PUT payload). Block2 controls response body transfer (GET response). Both use same mechanism.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro