Skip to content

CoAP POST Request Not Creating Resource

DodaTech Updated 2026-06-26 1 min read

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

The Problem

CoAP POST creates a resource but the server returns 4.00 Bad Request.

Quick Fix

Wrong

server.on('request', (req, res) => {
  // No path matching — all requests same handler
  res.code = '2.01'  # Created
  res.end(req.payload)
})
Client receives 2.01 Created but resource path is not returned.
server.on('request', (req, res) => {
  if (req.method === 'POST' && req.url === '/devices') {
    const deviceId = 'device-' + Date.now()
    
    // Store resource
    devices[deviceId] = JSON.parse(req.payload.toString())
    
    // Return 2.01 Created with location
    res.code = '2.01'
    res.setOption('Location-Path', ['devices', deviceId])
    res.end(JSON.stringify({ id: deviceId }))
  } else if (req.method === 'POST') {
    res.code = '4.00'
    res.end('Bad Request: use /devices')
  }
})
Client receives 2.01 Created with Location-Path option pointing to new resource.

Prevention

CoAP POST creates a sub-resource (like HTTP POST). Return 2.01 Created with Location-Path option. Validate the request payload before creating. CoAP PUT replaces or creates at the specified path (idempotent).

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What is the difference between POST and PUT?

POST creates a new resource under the URL (server assigns ID). PUT creates/replaces at the exact URL (client assigns ID).

What is Location-Path?

A CoAP option that tells the client where the new resource is located. Similar to HTTP Location header. The path is relative to the request URL.

Can I return 2.01 without a payload?

Yes. 2.01 Created can have no payload and just the Location-Path option. The subscriber discovers the new resource.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro