Skip to content

Coap Get

DodaTech 1 min read

In this tutorial, you'll learn about CoAP GET 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 a success code but empty payload.

Quick Fix

Wrong

server.on('request', (req, res) => {
  res.code = '2.05'
  res.end()  # No payload
})
Client gets 2.05 Content with empty payload.
const coap = require('coap')

const server = coap.createServer()

server.on('request', (req, res) => {
  if (req.url === '/status') {
    const payload = JSON.stringify({
      uptime: process.uptime(),
      memory: process.memoryUsage(),
      temp: 25.5
    })
    
    res.code = '2.05'
    res.setOption('Content-Format', 'application/json')
    res.end(payload)
  } else {
    res.code = '4.04'
    res.end('Not Found')
  }
})

server.listen(5683)
Client receives 2.05 Content with JSON payload of status data.

Prevention

Always include a payload with 2.05 responses (or 2.03 for no-body). Set Content-Format option: 0 (text/plain), 50 (application/json), 40 (application/link-format). Check res.end() — it must receive a string or Buffer. Use appropriate response codes.

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

FAQ

### What Content-Format values are common?

0 = text/plain, 40 = application/link-format, 41 = application/xml, 42 = application/octet-stream, 47 = application/exi, 50 = application/json, 60 = application/cbor.

Can CoAP return binary data?

Yes. Any binary payload is supported. Set Content-Format to 42 and pass a Buffer.

How does CoAP handle large responses?

Use Block-wise transfer (Block2 option). The server sends the response in 1 KB blocks with a block number for each.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro