Update http request node to support Property In/Out

This commit is contained in:
Steve-Mcl 2024-04-12 11:02:26 +01:00
parent dc36975083
commit f1e54c959b
2 changed files with 64 additions and 34 deletions

View File

@ -15,6 +15,16 @@
--> -->
<script type="text/html" data-template-name="http request"> <script type="text/html" data-template-name="http request">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-sign-in"></i> <span data-i18n="common.label.propertyIn"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-method"><i class="fa fa-tasks"></i> <span data-i18n="httpin.label.method"></span></label> <label for="node-input-method"><i class="fa fa-tasks"></i> <span data-i18n="httpin.label.method"></span></label>
<select type="text" id="node-input-method" style="width:70%;"> <select type="text" id="node-input-method" style="width:70%;">
@ -98,7 +108,7 @@
<div class="form-row"> <div class="form-row">
<label for="node-input-ret"><i class="fa fa-arrow-left"></i> <span data-i18n="httpin.label.return"></span></label> <label for="node-input-ret"><i class="fa fa-sign-out"></i> <span data-i18n="httpin.label.return"></span></label>
<select type="text" id="node-input-ret" style="width:70%;"> <select type="text" id="node-input-ret" style="width:70%;">
<option value="txt" data-i18n="httpin.utf8"></option> <option value="txt" data-i18n="httpin.utf8"></option>
<option value="bin" data-i18n="httpin.binary"></option> <option value="bin" data-i18n="httpin.binary"></option>
@ -107,7 +117,10 @@
</div> </div>
<div class="form-row form-tips" id="tip-json" hidden><span data-i18n="httpin.tip.req"></span></div> <div class="form-row form-tips" id="tip-json" hidden><span data-i18n="httpin.tip.req"></span></div>
<div class="form-row">
<label for="node-input-propertyOut"><i class="fa fa-sign-out"></i> <span data-i18n="common.label.propertyOut"></span></label>
<input type="text" id="node-input-propertyOut" style="width:70%;"/>
</div>
<div class="form-row" style="margin-bottom:0;"> <div class="form-row" style="margin-bottom:0;">
<label><i class="fa fa-list"></i> <span data-i18n="httpin.label.headers"></span></label> <label><i class="fa fa-list"></i> <span data-i18n="httpin.label.headers"></span></label>
</div> </div>
@ -115,10 +128,7 @@
<ol id="node-input-headers-container"></ol> <ol id="node-input-headers-container"></ol>
</div> </div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -235,7 +245,13 @@
insecureHTTPParser: {value: false}, insecureHTTPParser: {value: false},
authType: {value: ""}, authType: {value: ""},
senderr: {value: false}, senderr: {value: false},
headers: { value: [] } headers: { value: [] },
property: {value:"payload",required:true,
validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true }),
label:RED._("node-red:common.label.propertyIn")},
propertyOut: {value:"payload",required:true,
validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true}),
label:RED._("node-red:common.label.propertyOut")}
}, },
credentials: { credentials: {
user: {type:"text"}, user: {type:"text"},
@ -426,6 +442,15 @@
headerList.editableList('addItem', node.headers[index]); headerList.editableList('addItem', node.headers[index]);
} }
} }
if (this.property === undefined) {
$("#node-input-property").val("payload");
}
$("#node-input-property").typedInput({default:'msg',types:['msg']});
if (this.propertyOut === undefined) {
$("#node-input-propertyOut").val("payload");
}
$("#node-input-propertyOut").typedInput({default:'msg',types:['msg']});
}, },
oneditsave: function() { oneditsave: function() {
if (!$("#node-input-usetls").is(':checked')) { if (!$("#node-input-usetls").is(':checked')) {

View File

@ -68,6 +68,8 @@ in your Node-RED user directory (${RED.settings.userDir}).
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
checkNodeAgentPatch(); checkNodeAgentPatch();
const node = this; const node = this;
node.property = n.property||"payload";
node.propertyOut = n.propertyOut||node.property;
const nodeUrl = n.url; const nodeUrl = n.url;
const isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1; const isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
const nodeMethod = n.method || "GET"; const nodeMethod = n.method || "GET";
@ -431,14 +433,15 @@ in your Node-RED user directory (${RED.settings.userDir}).
} }
} }
var payload = null; var payload = null;
const value = RED.util.getMessageProperty(msg, node.property)
if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") { if (method !== 'GET' && method !== 'HEAD' && typeof value !== "undefined") {
if (opts.headers['content-type'] == 'multipart/form-data' && typeof msg.payload === "object") { if (opts.headers['content-type'] == 'multipart/form-data' && typeof value === "object") {
let formData = new FormData(); let formData = new FormData();
for (var opt in msg.payload) { for (var opt in value) {
if (msg.payload.hasOwnProperty(opt)) { if (value.hasOwnProperty(opt)) {
var val = msg.payload[opt]; var val = value[opt];
if (val !== undefined && val !== null) { if (val !== undefined && val !== null) {
if (typeof val === 'string' || Buffer.isBuffer(val)) { if (typeof val === 'string' || Buffer.isBuffer(val)) {
formData.append(opt, val); formData.append(opt, val);
@ -455,15 +458,15 @@ in your Node-RED user directory (${RED.settings.userDir}).
delete opts.headers['content-type']; delete opts.headers['content-type'];
opts.body = formData; opts.body = formData;
} else { } else {
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) { if (typeof value === "string" || Buffer.isBuffer(value)) {
payload = msg.payload; payload = value;
} else if (typeof msg.payload == "number") { } else if (typeof value == "number") {
payload = msg.payload+""; payload = value + "";
} else { } else {
if (opts.headers['content-type'] == 'application/x-www-form-urlencoded') { if (opts.headers['content-type'] == 'application/x-www-form-urlencoded') {
payload = querystring.stringify(msg.payload); payload = querystring.stringify(value);
} else { } else {
payload = JSON.stringify(msg.payload); payload = JSON.stringify(value);
if (opts.headers['content-type'] == null) { if (opts.headers['content-type'] == null) {
opts.headers[ctSet] = "application/json"; opts.headers[ctSet] = "application/json";
} }
@ -481,13 +484,13 @@ in your Node-RED user directory (${RED.settings.userDir}).
} }
if (method == 'GET' && typeof msg.payload !== "undefined" && paytoqs) { if (method == 'GET' && typeof value !== "undefined" && paytoqs) {
if (typeof msg.payload === "object") { if (typeof value === "object") {
try { try {
if (url.indexOf("?") !== -1) { if (url.indexOf("?") !== -1) {
url += (url.endsWith("?")?"":"&") + querystring.stringify(msg.payload); url += (url.endsWith("?")?"":"&") + querystring.stringify(value);
} else { } else {
url += "?" + querystring.stringify(msg.payload); url += "?" + querystring.stringify(value);
} }
} catch(err) { } catch(err) {
@ -501,14 +504,14 @@ in your Node-RED user directory (${RED.settings.userDir}).
nodeDone(); nodeDone();
return; return;
} }
} else if ( method == "GET" && typeof msg.payload !== "undefined" && paytobody) { } else if ( method == "GET" && typeof value !== "undefined" && paytobody) {
opts.allowGetBody = true; opts.allowGetBody = true;
if (typeof msg.payload === "object") { if (typeof value === "object") {
opts.body = JSON.stringify(msg.payload); opts.body = JSON.stringify(value);
} else if (typeof msg.payload == "number") { } else if (typeof value == "number") {
opts.body = msg.payload+""; opts.body = value + "";
} else if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) { } else if (typeof value === "string" || Buffer.isBuffer(value)) {
opts.body = msg.payload; opts.body = value;
} }
} }
@ -597,7 +600,7 @@ in your Node-RED user directory (${RED.settings.userDir}).
msg.statusCode = res.statusCode; msg.statusCode = res.statusCode;
msg.headers = res.headers; msg.headers = res.headers;
msg.responseUrl = res.url; msg.responseUrl = res.url;
msg.payload = res.body; let result = res.body;
msg.redirectList = redirectList; msg.redirectList = redirectList;
msg.retry = 0; msg.retry = 0;
@ -622,14 +625,15 @@ in your Node-RED user directory (${RED.settings.userDir}).
// Convert the payload to the required return type // Convert the payload to the required return type
if (node.ret !== "bin") { if (node.ret !== "bin") {
msg.payload = msg.payload.toString('utf8'); // txt result = result.toString('utf8'); // txt
if (node.ret === "obj") { if (node.ret === "obj") {
if (msg.statusCode == 204){msg.payload= "{}"}; if (msg.statusCode == 204){result= "{}"};
try { msg.payload = JSON.parse(msg.payload); } // obj try { result = JSON.parse(result); } // obj
catch(e) { node.warn(RED._("httpin.errors.json-error")); } catch(e) { node.warn(RED._("httpin.errors.json-error")); }
} }
} }
RED.util.setMessageProperty(msg, node.propertyOut, result)
node.status({}); node.status({});
nodeSend(msg); nodeSend(msg);
nodeDone(); nodeDone();
@ -647,7 +651,8 @@ in your Node-RED user directory (${RED.settings.userDir}).
node.error(err,msg); node.error(err,msg);
node.status({fill:"red", shape:"ring", text:err.code}); node.status({fill:"red", shape:"ring", text:err.code});
} }
msg.payload = err.toString() + " : " + url; result = err.toString() + " : " + url;
RED.util.setMessageProperty(msg, node.propertyOut, result)
msg.statusCode = err.code || (err.response?err.response.statusCode:undefined); msg.statusCode = err.code || (err.response?err.response.statusCode:undefined);
if (node.metric() && timingLog) { if (node.metric() && timingLog) {
emitTimingMetricLog(err.timings, msg); emitTimingMetricLog(err.timings, msg);