1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Tidy up HTTP Request payload to GET params work

This commit is contained in:
Nick O'Leary 2019-02-04 21:30:11 +00:00
parent 884b8da8bf
commit 4de1056d82
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 49 additions and 17 deletions

View File

@ -31,9 +31,9 @@
<input id="node-input-url" type="text" placeholder="http://"> <input id="node-input-url" type="text" placeholder="http://">
</div> </div>
<div class="form-row node-input-usePayloadAsParameters-row"> <div class="form-row node-input-paytoqs-row">
<input type="checkbox" id="node-input-usePayloadAsParameters" style="display: inline-block; width: auto; vertical-align: top;"> <input type="checkbox" id="node-input-paytoqs" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-usePayloadAsParameters" style="width: auto" data-i18n="httpin.label.usePayloadAsParameters"></label> <label for="node-input-paytoqs" style="width: auto" data-i18n="httpin.label.paytoqs"></label>
</div> </div>
<div class="form-row"> <div class="form-row">
@ -90,7 +90,7 @@
name: {value:""}, name: {value:""},
method:{value:"GET"}, method:{value:"GET"},
ret: {value:"txt"}, 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)} }, url:{value:"",validate:function(v) { return (v.trim().length === 0) || (v.indexOf("://") === -1) || (v.trim().indexOf("http") === 0)} },
tls: {type:"tls-config",required: false}, tls: {type:"tls-config",required: false},
proxy: {type:"http proxy",required: false} proxy: {type:"http proxy",required: false}
@ -123,9 +123,9 @@
}); });
$("#node-input-method").change(function() { $("#node-input-method").change(function() {
if ($(this).val() == "GET") { if ($(this).val() == "GET") {
$(".node-input-usePayloadAsParameters-row").show(); $(".node-input-paytoqs-row").show();
} else { } else {
$(".node-input-usePayloadAsParameters-row").hide(); $(".node-input-paytoqs-row").hide();
} }
}); });
if (this.credentials.user || this.credentials.has_password) { if (this.credentials.user || this.credentials.has_password) {

View File

@ -28,7 +28,7 @@ module.exports = function(RED) {
var nodeUrl = n.url; var nodeUrl = n.url;
var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1; var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
var nodeMethod = n.method || "GET"; var nodeMethod = n.method || "GET";
var usePayloadAsParameters = n.usePayloadAsParameters; var paytoqs = n.paytoqs;
if (n.tls) { if (n.tls) {
var tlsNode = RED.nodes.getNode(n.tls); var tlsNode = RED.nodes.getNode(n.tls);
} }
@ -208,21 +208,24 @@ module.exports = function(RED) {
opts.body = payload; 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 (typeof msg.payload === "object") {
if(opts.url.indexOf("?") !== -1) { try {
opts.url += "&" + querystring.stringify(msg.payload); if (opts.url.indexOf("?") !== -1) {
} else { opts.url += (opts.url.endsWith("?")?"":"&") + querystring.stringify(msg.payload);
opts.url += "?" + querystring.stringify(msg.payload); } else {
opts.url += "?" + querystring.stringify(msg.payload);
}
} catch(err) {
node.error(RED._("httpin.errors.invalid-payload"),msg);
return;
} }
} else { } else {
//I'm not sure where to set "httpin.errors.unvalid-payload" :(
node.error(RED._("httpin.errors.invalid-payload"),msg); node.error(RED._("httpin.errors.invalid-payload"),msg);
return; return;
} }
} }
// revert to user supplied Capitalisation if needed. // revert to user supplied Capitalisation if needed.
if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) { if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) {
opts.headers[ctSet] = opts.headers['content-type']; opts.headers[ctSet] = opts.headers['content-type'];

View File

@ -378,7 +378,7 @@
"status": "Status code", "status": "Status code",
"headers": "Headers", "headers": "Headers",
"other": "other", "other": "other",
"usePayloadAsParameters" : "Use payload properties as parameters" "paytoqs" : "Append msg.payload as query string parameters"
}, },
"setby": "- set by msg.method -", "setby": "- set by msg.method -",
"basicauth": "Use basic authentication", "basicauth": "Use basic authentication",

View File

@ -218,6 +218,12 @@ describe('HTTP Request Node', function() {
res.cookie('redirectReturn','return1'); res.cookie('redirectReturn','return1');
res.status(200).end(); res.status(200).end();
}); });
testApp.get('/getQueryParams', function(req,res) {
res.json({
query:req.query,
url: req.originalUrl
});
})
startServer(function(err) { startServer(function(err) {
if (err) { if (err) {
done(err); done(err);
@ -236,7 +242,6 @@ describe('HTTP Request Node', function() {
}); });
}); });
beforeEach(function() { beforeEach(function() {
preEnvHttpProxyLowerCase = process.env.http_proxy; preEnvHttpProxyLowerCase = process.env.http_proxy;
preEnvHttpProxyUpperCase = 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() { describe('HTTP header', function() {
it('should receive cookie', function(done) { it('should receive cookie', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/setCookie')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/setCookie')},