Skip to content

CoAP GET Request Returns Empty Response

DodaTech Updated 2026-06-26 1 min read

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

The Problem

CoAP GET request returns success code 2.05 but empty payload.

Quick Fix

Wrong

coap.get('coap://server/temp', (res) => {
  console.log(res.payload)  // undefined
})```

Client receives 2.05 Content with empty payload.


### Right

```cpp
#include <coap.h>

coap_context_t *ctx = coap_new_context(NULL);

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}";
  coap_add_data_blocked_response(response, __FILE__, __LINE__,
                                 buf, sizeof(buf) - 1,
                                 COAP_MEDIATYPE_APPLICATION_JSON, 0);
}

int main() {
  coap_resource_t *res = coap_resource_init(NULL, 0, 0);
  coap_register_handler(res, COAP_REQUEST_GET, get_handler);
  coap_add_resource(ctx, res);
  coap_start(ctx);
}```

Client receives 2.05 Content with JSON payload: {"temp":25.5}


## Prevention

CoAP GET requests expect a response payload with 2.05 (Content). Always include payload data and set Content-Format. Common formats: 0 (text/plain), 50 (application/json), 40 (application/link-format). Use 2.03 (Valid) only when no body is needed. CoAP runs over UDP port 5683 by default.

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 Content-Format values?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>0=text/plain, 40=application/link-format, 41=application/xml, 42=application/octet-stream, 50=application/json, 60=application/cbor.</p>
<h3 id="can-coap-return-binary">Can CoAP return binary?</h3><p>Yes. Set Content-Format to 42 and pass a Buffer. Use Block-wise for large payloads.</p>
<h3 id="how-are-large-responses-handled">How are large responses handled?</h3><p>Block-wise transfer (Block2 option). Server sends in ~1 KB blocks with MORE flag. Client requests next block.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro