I am doing a COAP service call using the following code segment and getting the response. According to the code, I can print the response in the log.
var req = coap.request('coap://localhost/hello')
req.on('response', function(res) {
res.pipe(process.stdout);
});
I need to get the response string to a variable for further processing. I tried console.log(res) and got following on the log where I cannot find the response string in. How can I get the response string (body) to a variable instead of printing it on the log? I think it is wrapped inside payload
. I need it as a string.
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
payload: <Buffer 48 65 6c 6c 6f 20 74 68 65 72 65>,
options: [],
code: '2.05',
method: undefined,
headers: {},
url: '/',
rsinfo: { address: '127.0.0.1', family: 'IPv4', port: 5683, size: 20 },
outSocket: { address: '0.0.0.0', family: 'IPv4', port: 41185 },
_packet:
{ code: '2.05',
confirmable: false,
reset: false,
ack: true,
messageId: 24618,
token: <Buffer d8 41 e0 57>,
options: [],
payload: <Buffer 48 65 6c 6c 6f 20 74 68 65 72 65> },
_payloadIndex: 0 }