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

Simple fix to check/force http: to close #288

Checks url starts http: or https: - if not add http: - at least then it breaks gently.
Also add resultant url to any error payload if request  fails in order to make debug easier.
This commit is contained in:
Dave C-J 2014-07-23 12:09:21 +01:00
parent 14a1899c23
commit 82036dd84a
2 changed files with 10 additions and 5 deletions

View File

@ -121,8 +121,9 @@
<p>The URL and HTTP method can be configured in the node, but also
overridden by the incoming message:
<ul>
<li><code>url</code>, if set, is used as the url of the request</li>
<li><code>method</code>, if set, is used as the HTTP method of the request. Must be one of <code>GET</code>, <code>PUT</code>, <code>POST</code> or <code>DELETE</code> (default: GET)</li>
<li><code>url</code>, if set, is used as the url of the request. Must start with http: or https:</li>
<li><code>method</code>, if set, is used as the HTTP method of the request.
Must be one of <code>GET</code>, <code>PUT</code>, <code>POST</code> or <code>DELETE</code> (default: GET)</li>
<li><code>headers</code>, if set, should be an object containing field/value
pairs to be added as request headers</li>
<li><code>payload</code> is sent as the body of the request</li>

View File

@ -16,7 +16,6 @@
module.exports = function(RED) {
"use strict";
var util = require("util");
var http = require("follow-redirects").http;
var https = require("follow-redirects").https;
var urllib = require("url");
@ -159,6 +158,11 @@ module.exports = function(RED) {
} else {
url = nodeUrl;
}
// url must start http:// or https:// so assume http:// if not set
if (!((url.indexOf("http://")===0) || (url.indexOf("https://")===0))) {
url = "http://"+url;
}
var method = (msg.method||nodeMethod).toUpperCase();
//node.log(method+" : "+url);
var opts = urllib.parse(url);
@ -208,7 +212,7 @@ module.exports = function(RED) {
});
});
req.on('error',function(err) {
msg.payload = err.toString();
msg.payload = err.toString() + " : " + url;
msg.statusCode = err.code;
node.send(msg);
node.status({fill:"red",shape:"ring",text:err.code});
@ -219,7 +223,7 @@ module.exports = function(RED) {
req.end();
});
}
RED.nodes.registerType("http request",HTTPRequest,{
credentials: {
user: {type:"text"},