add msg.payloadHandling for get requests

This commit is contained in:
Stephen McLaughlin
2022-11-28 19:31:44 +00:00
committed by GitHub
parent 14c362d4ba
commit 3ed5969e87
2 changed files with 109 additions and 21 deletions

View File

@@ -69,8 +69,6 @@ in your Node-RED user directory (${RED.settings.userDir}).
var nodeUrl = n.url;
var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
var nodeMethod = n.method || "GET";
var paytoqs = false;
var paytobody = false;
var redirectList = [];
var sendErrorsToCatch = n.senderr;
node.headers = n.headers || [];
@@ -78,15 +76,12 @@ in your Node-RED user directory (${RED.settings.userDir}).
if (n.tls) {
var tlsNode = RED.nodes.getNode(n.tls);
}
this.ret = n.ret || "txt";
this.authType = n.authType || "basic";
if (RED.settings.httpRequestTimeout) { this.reqTimeout = parseInt(RED.settings.httpRequestTimeout) || 120000; }
else { this.reqTimeout = 120000; }
if (n.paytoqs === true || n.paytoqs === "query") { paytoqs = true; }
else if (n.paytoqs === "body") { paytobody = true; }
node.ret = n.ret || "txt";
node.authType = n.authType || "basic";
if (RED.settings.httpRequestTimeout) { node.reqTimeout = parseInt(RED.settings.httpRequestTimeout) || 120000; }
else { node.reqTimeout = 120000; }
node.insecureHTTPParser = n.insecureHTTPParser
node.paytoqs = n.paytoqs
var prox, noprox;
if (process.env.http_proxy) { prox = process.env.http_proxy; }
@@ -196,20 +191,20 @@ in your Node-RED user directory (${RED.settings.userDir}).
}
}
var method = nodeMethod.toUpperCase() || "GET";
/** @type {boolean|'query'|'body'} */
let payloadHandling = node.paytoqs
let method = nodeMethod.toUpperCase() || "GET";
if (msg.method && n.method && (n.method !== "use")) { // warn if override option not set
node.warn(RED._("common.errors.nooverride"));
}
if (msg.method && n.method && (n.method === "use")) {
method = msg.method.toUpperCase(); // use the msg parameter
method = msg.method.toUpperCase(); // use the msg parameter
payloadHandling = msg.payloadHandling
}
if (payloadHandling === true) { payloadHandling = "query" }
// var isHttps = (/^https/i.test(url));
/** @type {import('got').Options} */
var opts = {};
// set defaultport, else when using HttpsProxyAgent, it's defaultPort of 443 will be used :(.
// Had to remove this to get http->https redirect to work
// opts.defaultPort = isHttps?443:80;
opts.timeout = node.reqTimeout;
opts.throwHttpErrors = false;
// TODO: add UI option to auto decompress. Setting to false for 1.x compatibility
@@ -472,7 +467,7 @@ in your Node-RED user directory (${RED.settings.userDir}).
}
if (method == 'GET' && typeof msg.payload !== "undefined" && paytoqs) {
if (method == "GET" && typeof msg.payload !== "undefined" && payloadHandling === "query") {
if (typeof msg.payload === "object") {
try {
if (url.indexOf("?") !== -1) {
@@ -481,18 +476,16 @@ in your Node-RED user directory (${RED.settings.userDir}).
url += "?" + querystring.stringify(msg.payload);
}
} catch(err) {
node.error(RED._("httpin.errors.invalid-payload"),msg);
nodeDone();
return;
}
} else {
node.error(RED._("httpin.errors.invalid-payload"),msg);
nodeDone();
return;
}
} else if ( method == "GET" && typeof msg.payload !== "undefined" && paytobody) {
} else if ( method == "GET" && typeof msg.payload !== "undefined" && payloadHandling === "body") {
opts.allowGetBody = true;
if (typeof msg.payload === "object") {
opts.body = JSON.stringify(msg.payload);