diff --git a/coap/coap-request.js b/coap/coap-request.js index 6147d35..1f8dd3d 100644 --- a/coap/coap-request.js +++ b/coap/coap-request.js @@ -63,7 +63,7 @@ module.exports = function (RED) { }; reqOpts.method = config.method.toUpperCase() || "GET"; if (config.method === "use" && msg.method != null) { - reqOpts = msg.method.toUpperCase(); + reqOpts.method = msg.method.toUpperCase(); } reqOpts.headers = msg.headers; @@ -98,7 +98,7 @@ module.exports = function (RED) { _send(data); } else if (contentFormat === "text/plain" || configContentFormat === "text/plain") { _send(data.toString()); - } else if (contentFormat.startsWith("application/") && contentFormat.endsWith("json")) { + } else if (contentFormat.startsWith("application/") && contentFormat.includes("json")) { try { _send(JSON.parse(data.toString())); } catch (error) { @@ -109,7 +109,7 @@ module.exports = function (RED) { }); node.error(error.message); } - } else if (contentFormat.startsWith("application/") && contentFormat.endsWith("cbor")) { + } else if (contentFormat.startsWith("application/") && contentFormat.includes("cbor")) { cbor.decodeAll(data, function (error, data) { if (error) { node.error(error.message); @@ -144,7 +144,7 @@ module.exports = function (RED) { var payload; if (reqOpts.method !== "GET") { - payload = _constructPayload(msg, config["content-format"]); + payload = _constructPayload(msg, reqOpts.headers["Content-Format"]); } else if (paytoqs === "query") { try { _appendQueryParams(reqOpts, msg.payload); diff --git a/test/coap-request_spec.js b/test/coap-request_spec.js index 58ba0fb..00cfc55 100644 --- a/test/coap-request_spec.js +++ b/test/coap-request_spec.js @@ -141,7 +141,7 @@ describe("CoapRequestNode", function () { id: "n3", type: "coap request", "content-format": "text/plain", - method: "", + method: "use", name: "coapRequest", observe: false, url: "coap://localhost:" + port + "/test-resource", @@ -619,8 +619,18 @@ describe("CoapRequestNode", function () { id: "n1", type: "inject", name: "Fire once", - payload: test.message, - payloadType: "string", + props: [ + { + "p": "payload", + "vt": "string", + "v": test.message + }, + { + "p": "headers", + "v": {"Content-Format": test.format}, + "vt": "object" + } + ], repeat: "", crontab: "", once: true, @@ -629,7 +639,6 @@ describe("CoapRequestNode", function () { { id: "n2", type: "coap request", - "content-format": test.format, method: "POST", name: "coapRequestPost", observe: false,