I think that's possibly because you havent set the correct Headers in your request
Try doing your fetch call like this :
let requestOptions = {
method: 'POST',
headers: { 'Content-Type':'application/json'},
body: { "Body": "MyBody"},
};
fetch("http://<yourip>:1880/ButtonReceiver", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => console.log('error', error));
also because fetch takes some time to complete (asynchronous) it always returns a Promise
so in order to get the response you need to tag on a .then
.
Now if your ButtonReceiver endpoint replies back with a json that needs to be parsed response.json()
but because .json
can also take some time that also returns a Promise .. yea i know .. and you need a second
.then
to return the actual data.
Modified the flow a bit to return a json and set your debug node to Complete msg.
[{"id":"e2fcc8b37c222ede","type":"debug","z":"4895ea10b4ee9ead","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":410,"y":940,"wires":[]},{"id":"86ea9e9c9abe64d8","type":"http response","z":"4895ea10b4ee9ead","name":"output","statusCode":"","headers":{},"x":670,"y":1020,"wires":[]},{"id":"8c2d3262.4f0a5","type":"http in","z":"4895ea10b4ee9ead","name":"HTTP IN","url":"/ButtonReceiver","method":"post","upload":false,"swaggerDoc":"","x":270,"y":1020,"wires":[["e2fcc8b37c222ede","6cd9b824e2102efa"]]},{"id":"6cd9b824e2102efa","type":"function","z":"4895ea10b4ee9ead","name":"","func":"msg.statusCode = 200\nmsg.payload = {\n \"message\": \"Message Received\",\n \"data\": msg.payload\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":1020,"wires":[["86ea9e9c9abe64d8"]]}]
Some reading material and examples on Fetch