mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Compare commits
	
		
			3 Commits
		
	
	
		
			4213-fix-s
			...
			3957-http-
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 20a63a3292 | ||
|  | 3a5cdbc8ae | ||
|  | 3ed5969e87 | 
| @@ -33,11 +33,12 @@ | ||||
|     </div> | ||||
|  | ||||
|     <div class="form-row node-input-paytoqs-row"> | ||||
|         <label for="node-input-paytoqs"><span data-i18n="common.label.payload"></span></label> | ||||
|         <label for="node-input-paytoqs"><span data-i18n="httpin.label.paytoqs.label"></span></label> | ||||
|         <select id="node-input-paytoqs" style="width: 70%;"> | ||||
|             <option value="ignore" data-i18n="httpin.label.paytoqs.ignore"></option> | ||||
|             <option value="query" data-i18n="httpin.label.paytoqs.query"></option> | ||||
|             <option value="body" data-i18n="httpin.label.paytoqs.body"></option> | ||||
|             <option value="setby" data-i18n="httpin.label.paytoqs.setby"></option> | ||||
|         </select> | ||||
|     </div> | ||||
|  | ||||
| @@ -290,7 +291,7 @@ | ||||
|                 RED.tray.resize(); | ||||
|             }); | ||||
|             $("#node-input-method").on("change", function() { | ||||
|                 if ($(this).val() == "GET") { | ||||
|                 if ($(this).val() == "GET" || $(this).val() == "use") { | ||||
|                     $(".node-input-paytoqs-row").show(); | ||||
|                 } else { | ||||
|                     $(".node-input-paytoqs-row").hide(); | ||||
|   | ||||
| @@ -16,7 +16,7 @@ | ||||
|  | ||||
| module.exports = function(RED) { | ||||
|     "use strict"; | ||||
|     const got = require("got"); | ||||
|     const got = require("got").default; | ||||
|     const {CookieJar} = require("tough-cookie"); | ||||
|     const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent'); | ||||
|     const FormData = require('form-data'); | ||||
| @@ -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,26 @@ in your Node-RED user directory (${RED.settings.userDir}). | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             var method = nodeMethod.toUpperCase() || "GET"; | ||||
|             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 | ||||
|             } | ||||
|              | ||||
|             /** @type {boolean|'query'|'body'|'setby'} */ | ||||
|             let payloadHandling = node.paytoqs | ||||
|             if (msg.payloadHandling && payloadHandling && (payloadHandling !== "setby")) {     // warn if override option not set | ||||
|                 node.warn(RED._("common.errors.nooverride")); | ||||
|             } | ||||
|             if (msg.payloadHandling && payloadHandling && (payloadHandling === "setby")) { | ||||
|                 payloadHandling = msg.payloadHandling // use the msg parameter | ||||
|             } | ||||
|             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 +473,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 +482,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); | ||||
|   | ||||
| @@ -449,9 +449,11 @@ | ||||
|             "headers": "Kopfzeilen", | ||||
|             "other": "andere", | ||||
|             "paytoqs": { | ||||
|                 "label": "Payload (GET)", | ||||
|                 "ignore": "Ignorieren", | ||||
|                 "query": "Anfügen an query-string-Parameter", | ||||
|                 "body": "Senden als request-body" | ||||
|                 "body": "Senden als request-body", | ||||
|                 "setby": "Durch msg.payloadHandling festgelegt" | ||||
|             }, | ||||
|             "utf8String": "UTF-8-String", | ||||
|             "binaryBuffer": "Binärer Buffer", | ||||
|   | ||||
| @@ -509,9 +509,11 @@ | ||||
|             "headers": "Headers", | ||||
|             "other": "other", | ||||
|             "paytoqs": { | ||||
|                 "label": "Payload (GET)", | ||||
|                 "ignore": "Ignore", | ||||
|                 "query": "Append to query-string parameters", | ||||
|                 "body": "Send as request body" | ||||
|                 "body": "Send as request body", | ||||
|                 "setby": "- set by msg.payloadHandling -" | ||||
|             }, | ||||
|             "utf8String": "UTF8 string", | ||||
|             "binaryBuffer": "binary buffer", | ||||
|   | ||||
| @@ -30,6 +30,10 @@ | ||||
|         <dd>If set, can be used to send cookies with the request.</dd> | ||||
|         <dt class="optional">payload</dt> | ||||
|         <dd>Sent as the body of the request.</dd> | ||||
|         <dt class="optional">payloadHandling <span class="property-type">string</span></dt> | ||||
|         <dd>Only valid with GET requests. If set to <b>"- use msg.payloadHandling -"</b> in the node configuration, this property  | ||||
|             indicates how the <code>payload</code> will be sent. <code>msg.payloadHandling</code> should contain either | ||||
|             <code>"query"</code> or <code>"body"</code> otherwise the payload will not be sent with the GET request</dd> | ||||
|         <dt class="optional">rejectUnauthorized</dt> | ||||
|         <dd>If set to <code>false</code>, allows requests to be made to https sites that use | ||||
|             self signed certificates.</dd> | ||||
|   | ||||
| @@ -509,9 +509,11 @@ | ||||
|             "headers": "ヘッダ", | ||||
|             "other": "その他", | ||||
|             "paytoqs": { | ||||
|                 "label": "ペイロード (GET)", | ||||
|                 "ignore": "無視", | ||||
|                 "query": "クエリパラメータに追加", | ||||
|                 "body": "リクエストボディとして送信" | ||||
|                 "body": "リクエストボディとして送信", | ||||
|                 "setby": "- msg.payloadHandlingに定義 -" | ||||
|             }, | ||||
|             "utf8String": "UTF8文字列", | ||||
|             "binaryBuffer": "バイナリバッファ", | ||||
|   | ||||
| @@ -387,7 +387,13 @@ | ||||
|             "status": "상태코드", | ||||
|             "headers": "헤더", | ||||
|             "other": "그 외", | ||||
|             "paytoqs" : "msg.payload를 쿼리 파라미터에 추가", | ||||
|             "paytoqs": { | ||||
|                 "label": "페이로드(GET)", | ||||
|                 "ignore": "무시", | ||||
|                 "query": "msg.payload를 쿼리 파라미터에 추가", | ||||
|                 "body": "본문에 msg.payload 추가", | ||||
|                 "setby": "- msg.payloadHandling에 의해 설정됨 -" | ||||
|             }, | ||||
|             "utf8String": "UTF8문자열", | ||||
|             "binaryBuffer": "바이너리 버퍼", | ||||
|             "jsonObject": "JSON오브젝트", | ||||
|   | ||||
| @@ -411,9 +411,11 @@ | ||||
|             "headers": "Заголовки", | ||||
|             "other": "другое", | ||||
|             "paytoqs": { | ||||
|                 "label": "Данные (GET)", | ||||
|                 "ignore": "Игнорировать", | ||||
|                 "query": "Добавлять к параметрам строки запроса", | ||||
|                 "body": "Отправлять как тело запроса" | ||||
|                 "body": "Отправлять как тело запроса", | ||||
|                 "setby": "- устанавливается через msg.payloadHandling -" | ||||
|             }, | ||||
|             "utf8String": "Строка UTF8", | ||||
|             "binaryBuffer": "двоичный буфер", | ||||
|   | ||||
| @@ -407,7 +407,13 @@ | ||||
|             "status": "状态码", | ||||
|             "headers": "头", | ||||
|             "other": "其他", | ||||
|             "paytoqs": "将msg.payload附加为查询字符串参数", | ||||
|             "paytoqs": { | ||||
|                 "label": "有效负载(仅限 GET)", | ||||
|                 "ignore": "漠视", | ||||
|                 "query": "将msg.payload附加为查询字符串参数", | ||||
|                 "body": "在请求正文中发送负载", | ||||
|                 "setby": "- 用 msg.payloadHandling 设定 -" | ||||
|             }, | ||||
|             "utf8String": "UTF8格式的字符串", | ||||
|             "binaryBuffer": "二进制buffer", | ||||
|             "jsonObject": "解析的JSON对象", | ||||
|   | ||||
| @@ -411,7 +411,13 @@ | ||||
|             "status": "狀態碼", | ||||
|             "headers": "Header", | ||||
|             "other": "其他", | ||||
|             "paytoqs": "將msg.payload附加為查詢字符串參數", | ||||
|             "paytoqs": { | ||||
|                 "label": "有效負載(僅限 GET)", | ||||
|                 "ignore": "漠視", | ||||
|                 "query": "將msg.payload附加為查詢字符串參數", | ||||
|                 "body": "在請求正文中發送負載", | ||||
|                 "setby": "- 用 msg.payloadHandling 設定 -" | ||||
|             }, | ||||
|             "utf8String": "UTF8格式的字符串", | ||||
|             "binaryBuffer": "二進制buffer", | ||||
|             "jsonObject": "解析的JSON對象", | ||||
|   | ||||
| @@ -294,6 +294,19 @@ describe('HTTP Request Node', function() { | ||||
|                 url: req.originalUrl | ||||
|             }); | ||||
|         }) | ||||
|         testApp.get('/getBodyParams', function(req,res) { | ||||
|             // Either body-parser or express is discarding the body OR  | ||||
|             // GOT never sent a body. (there is nothing in params/query/body) | ||||
|             // Oddly, if we set options.json (instead of options.body) in the GOT | ||||
|             // request, then req.body will have the values! | ||||
|             //I suspect the GOT lib *is* sending body on a GET request since an  | ||||
|             // error is thrown if we try to set options.body on a GET request without | ||||
|             // setting options.allowGetBody to true. | ||||
|             res.json({ | ||||
|                 // body:JSON.parse(req.body), //req.body is empty! | ||||
|                 url: req.originalUrl | ||||
|             }); | ||||
|         }) | ||||
|         testApp.get('/returnError/:code', function(req,res) { | ||||
|             res.status(parseInt(req.params.code)).json({gotError:req.params.code}); | ||||
|         }) | ||||
| @@ -1117,6 +1130,89 @@ describe('HTTP Request Node', function() { | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         it('should allow the payload to be sent in the body for a GET request', function(done) { | ||||
|             var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",paytoqs:"body",ret:"obj",url:getTestURL('/getBodyParams')}, | ||||
|                 {id:"n2", type:"helper"}]; | ||||
|             helper.load(httpRequestNode, flow, function() { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 var n2 = helper.getNode("n2"); | ||||
|                 const payload = {a:"one",b:true,c:3} | ||||
|                 n2.on("input", function(msg) { | ||||
|                     try { | ||||
|                         // Either Express does not deliver the body of a GET request OR GOT doesn't send it! | ||||
|                         // That means we cannot test this!  Oddly, if the HTTP-Req node sets options.json instead  | ||||
|                         // of options.body, then the body is delivered.  | ||||
|                         // msg.should.have.property('payload',{ | ||||
|                         //     body:payload, | ||||
|                         //     url: '/getBodyParams' | ||||
|                         // }); | ||||
|                         msg.should.have.property('payload').and.be.an.Object() | ||||
|                         msg.payload.should.have.property('url', '/getBodyParams') | ||||
|  | ||||
|                         msg.should.have.property('statusCode',200); | ||||
|                         msg.should.have.property('headers'); | ||||
|                         done(); | ||||
|                     } catch(err) { | ||||
|                         done(err); | ||||
|                     } | ||||
|                 }); | ||||
|                 n1.receive({payload:payload}); | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         it('should allow the message to specify that the payload be append to querystring for a GET request', function(done) { | ||||
|             var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"use",paytoqs:"setby",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},method:"get",payloadHandling:'query'}); | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|  | ||||
|         it('should allow the message to specify that the payload be sent in the body for a GET request', function(done) { | ||||
|             var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"use",paytoqs:"setby",ret:"obj",url:getTestURL('/getBodyParams')}, | ||||
|                 {id:"n2", type:"helper"}]; | ||||
|             helper.load(httpRequestNode, flow, function() { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 var n2 = helper.getNode("n2"); | ||||
|                 const payload = {a:"one",b:true,c:3} | ||||
|                 n2.on("input", function(msg) { | ||||
|                     try { | ||||
|                         // Either Express does not deliver the body of a GET request OR GOT doesn't send it! | ||||
|                         // That means we cannot test this!  Oddly, if the HTTP-Req node sets options.json instead  | ||||
|                         // of options.body, then the body is delivered.  | ||||
|                         // msg.should.have.property('payload',{ | ||||
|                         //     payload: payload, | ||||
|                         //     url: '/getBodyParams' | ||||
|                         // }); | ||||
|                         msg.should.have.property('payload').and.be.an.Object() | ||||
|                         msg.payload.should.have.property('url', '/getBodyParams') | ||||
|                         msg.should.have.property('statusCode',200); | ||||
|                         msg.should.have.property('headers'); | ||||
|                         done(); | ||||
|                     } catch(err) { | ||||
|                         done(err); | ||||
|                     } | ||||
|                 }); | ||||
|                 n1.receive({payload:payload,method:"get",payloadHandling:'body'}); | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         it('should send a msg for non-2xx response status - 400', function(done) { | ||||
|             var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/returnError/400')}, | ||||
|             {id:"n2", type:"helper"}]; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user