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://">
</div>
<div class="form-row node-input-usePayloadAsParameters-row">
<input type="checkbox" id="node-input-usePayloadAsParameters" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-usePayloadAsParameters" style="width: auto" data-i18n="httpin.label.usePayloadAsParameters"></label>
<div class="form-row node-input-paytoqs-row">
<input type="checkbox" id="node-input-paytoqs" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-paytoqs" style="width: auto" data-i18n="httpin.label.paytoqs"></label>
</div>
<div class="form-row">
@ -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) {

View File

@ -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,16 +208,19 @@ 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;
}

View File

@ -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",

View File

@ -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')},