diff --git a/packages/node_modules/@node-red/nodes/core/io/21-httprequest.html b/packages/node_modules/@node-red/nodes/core/io/21-httprequest.html index 54f1086b4..a9c0f5544 100644 --- a/packages/node_modules/@node-red/nodes/core/io/21-httprequest.html +++ b/packages/node_modules/@node-red/nodes/core/io/21-httprequest.html @@ -31,9 +31,9 @@ -
- - +
+ +
@@ -90,7 +90,7 @@ name: {value:""}, method:{value:"GET"}, ret: {value:"txt"}, - usePayloadAsParameters: {value: false}, + paytoqs: {value: false}, url:{value:"",validate:function(v) { return (v.trim().length === 0) || (v.indexOf("://") === -1) || (v.trim().indexOf("http") === 0)} }, tls: {type:"tls-config",required: false}, proxy: {type:"http proxy",required: false} @@ -123,9 +123,9 @@ }); $("#node-input-method").change(function() { if ($(this).val() == "GET") { - $(".node-input-usePayloadAsParameters-row").show(); + $(".node-input-paytoqs-row").show(); } else { - $(".node-input-usePayloadAsParameters-row").hide(); + $(".node-input-paytoqs-row").hide(); } }); if (this.credentials.user || this.credentials.has_password) { diff --git a/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js b/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js index 1217d5fcc..cad6444a7 100644 --- a/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js +++ b/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js @@ -28,7 +28,7 @@ module.exports = function(RED) { var nodeUrl = n.url; var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1; var nodeMethod = n.method || "GET"; - var usePayloadAsParameters = n.usePayloadAsParameters; + var paytoqs = n.paytoqs; if (n.tls) { var tlsNode = RED.nodes.getNode(n.tls); } @@ -208,21 +208,24 @@ module.exports = function(RED) { opts.body = payload; } - if (method == 'GET' && typeof msg.payload !== "undefined" && usePayloadAsParameters) { + if (method == 'GET' && typeof msg.payload !== "undefined" && paytoqs) { if (typeof msg.payload === "object") { - if(opts.url.indexOf("?") !== -1) { - opts.url += "&" + querystring.stringify(msg.payload); - } else { - opts.url += "?" + querystring.stringify(msg.payload); + try { + if (opts.url.indexOf("?") !== -1) { + opts.url += (opts.url.endsWith("?")?"":"&") + querystring.stringify(msg.payload); + } else { + opts.url += "?" + querystring.stringify(msg.payload); + } + } catch(err) { + node.error(RED._("httpin.errors.invalid-payload"),msg); + return; } - } else { - //I'm not sure where to set "httpin.errors.unvalid-payload" :( node.error(RED._("httpin.errors.invalid-payload"),msg); return; } } - + // revert to user supplied Capitalisation if needed. if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) { opts.headers[ctSet] = opts.headers['content-type']; diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index 73afd926b..d88469297 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -378,7 +378,7 @@ "status": "Status code", "headers": "Headers", "other": "other", - "usePayloadAsParameters" : "Use payload properties as parameters" + "paytoqs" : "Append msg.payload as query string parameters" }, "setby": "- set by msg.method -", "basicauth": "Use basic authentication", diff --git a/test/nodes/core/io/21-httprequest_spec.js b/test/nodes/core/io/21-httprequest_spec.js index 04b191e2d..46a64b7fe 100644 --- a/test/nodes/core/io/21-httprequest_spec.js +++ b/test/nodes/core/io/21-httprequest_spec.js @@ -218,6 +218,12 @@ describe('HTTP Request Node', function() { res.cookie('redirectReturn','return1'); res.status(200).end(); }); + testApp.get('/getQueryParams', function(req,res) { + res.json({ + query:req.query, + url: req.originalUrl + }); + }) startServer(function(err) { if (err) { done(err); @@ -236,7 +242,6 @@ describe('HTTP Request Node', function() { }); }); - beforeEach(function() { preEnvHttpProxyLowerCase = process.env.http_proxy; preEnvHttpProxyUpperCase = process.env.HTTP_PROXY; @@ -970,7 +975,31 @@ describe('HTTP Request Node', function() { }); }); + + it('should append query params to url - obj', function(done) { + var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",paytoqs:true,ret:"obj",url:getTestURL('/getQueryParams')}, + {id:"n2", type:"helper"}]; + helper.load(httpRequestNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('payload',{ + query:{a:'1',b:'2',c:'3'}, + url: '/getQueryParams?a=1&b=2&c=3' + }); + msg.should.have.property('statusCode',200); + msg.should.have.property('headers'); + done(); + } catch(err) { + done(err); + } + }); + n1.receive({payload:{a:1,b:2,c:3}}); + }); + }); }); + describe('HTTP header', function() { it('should receive cookie', function(done) { var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/setCookie')},