Skip to content

CoAP PUT Request Fails to Update Resource

DodaTech Updated 2026-06-26 1 min read

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

The Problem

CoAP PUT request does not update the target resource as expected.

Quick Fix

Wrong

res.code = '2.04';
res.end();
// Resource not actually updated — ignored payload```

Client receives 2.04 Changed but resource value did not update.


### Right

```cpp
#include <coap.h>

static float current_temp = 0.0;

void put_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) {
  size_t size;
  const unsigned char *data = coap_get_data(request, &size);

  if (size == 0) {
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
    return;
  }

  // Parse and store the new value
  char buf[64];
  memcpy(buf, data, size);
  buf[size] = '';
  current_temp = atof(buf);

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
  coap_add_data(response, size, data);
}```

Client receives 2.04 Changed with echoed payload. GET /temperature now returns updated value.


## Prevention

CoAP PUT replaces or creates a resource at the specified path. Return 2.04 (Changed) for updates, 2.01 (Created) for new resources. Always parse and apply the payload before sending the response. PUT is idempotent — multiple identical PUTs produce the same result. Validate input to prevent data corruption.

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">### PUT vs POST update?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>PUT replaces the entire resource at the path. POST appends or processes. Use PUT for full updates, POST for actions or partial updates.</p>
<h3 id="what-response-code">What response code?</h3><p>2.04 Changed for update, 2.01 Created for new resource. Echo the payload to confirm what was stored.</p>
<h3 id="is-put-idempotent">Is PUT idempotent?</h3><p>Yes. Multiple identical PUT requests produce the same server state. POST is NOT idempotent.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro