From 2b01a3fcd36aa09eec0aafe47686ab01fbaead76 Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Wed, 2 Aug 2023 14:15:29 +0100 Subject: [PATCH 1/2] Handle 204 in httprequest JSON If the http statusCode is 204 (Success, No Content) and the node return type is set to JSON this sets msg.payload as an empty json object so as to supress the JSON parse error --- .../node_modules/@node-red/nodes/core/network/21-httprequest.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js b/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js index ad283657d..fe1884361 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js @@ -618,6 +618,7 @@ in your Node-RED user directory (${RED.settings.userDir}). msg.payload = msg.payload.toString('utf8'); // txt if (node.ret === "obj") { + if (msg.statusCode == 204){msg.payload= "{}"}; try { msg.payload = JSON.parse(msg.payload); } // obj catch(e) { node.warn(RED._("httpin.errors.json-error")); } } From ec86ec188b219ebd3e3e4f89d373553cd04d14fe Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Wed, 2 Aug 2023 14:32:39 +0100 Subject: [PATCH 2/2] Update Test I've changed the DELETE test to expect an empty object as the node is requesting an object response, this will therefore cover testing the new functionality. The subsequent HEAD test also expects a 204 response but the requested type is txt so that will still expect an empty string response. --- test/nodes/core/network/21-httprequest_spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nodes/core/network/21-httprequest_spec.js b/test/nodes/core/network/21-httprequest_spec.js index 14f3fd628..cc7de59d2 100644 --- a/test/nodes/core/network/21-httprequest_spec.js +++ b/test/nodes/core/network/21-httprequest_spec.js @@ -557,7 +557,7 @@ describe('HTTP Request Node', function() { var n2 = helper.getNode("n2"); n2.on("input", function(msg) { try { - msg.should.have.property('payload',''); + msg.should.have.property('payload',{}); msg.should.have.property('statusCode',204); done(); } catch(err) {