mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add new http request node
This commit is contained in:
parent
df205aab58
commit
d332600149
@ -76,7 +76,7 @@
|
|||||||
outputs:1,
|
outputs:1,
|
||||||
icon: "white-globe.png",
|
icon: "white-globe.png",
|
||||||
label: function() {
|
label: function() {
|
||||||
return this.name||"["+this.method+"] "+this.url;
|
return this.name||(this.url?("["+this.method+"] "+this.url):"http");
|
||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return this.name?"node_label_italic":"";
|
return this.name?"node_label_italic":"";
|
||||||
@ -85,14 +85,19 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-template-name="http response">
|
<script type="text/x-red" data-template-name="http response">
|
||||||
<div class="form-tips">The messages sent to this node must originate from an HTTP input node</div>
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
<div class="form-tips">The messages sent to this node <b>must</b> originate from an <i>http input</i> node</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="http response">
|
<script type="text/x-red" data-help-name="http response">
|
||||||
<p>Provides an response node for http requests received from an http input node.</p>
|
<p>Sends responses back to http requests received from an HTTP Input node.</p>
|
||||||
|
<p>The response can be customised using the following message properties:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li><code>payload</code> is sent as the body of the reponse</li>
|
<li><code>payload</code> is sent as the body of the reponse</li>
|
||||||
<li><code>rc</code>, if set, is used as the response code (default: 200)</li>
|
<li><code>statusCode</code>, if set, is used as the response status code (default: 200)</li>
|
||||||
<li><code>headers</code>, if set, should be an object containing field/value
|
<li><code>headers</code>, if set, should be an object containing field/value
|
||||||
pairs to be added as response headers.</li>
|
pairs to be added as response headers.</li>
|
||||||
</ul>
|
</ul>
|
||||||
@ -102,17 +107,89 @@
|
|||||||
RED.nodes.registerType('http response',{
|
RED.nodes.registerType('http response',{
|
||||||
category: 'output',
|
category: 'output',
|
||||||
color:"rgb(231, 231, 174)",
|
color:"rgb(231, 231, 174)",
|
||||||
defaults: {},
|
defaults: {
|
||||||
|
name: {value:""}
|
||||||
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:0,
|
outputs:0,
|
||||||
align: "right",
|
align: "right",
|
||||||
icon: "white-globe.png",
|
icon: "white-globe.png",
|
||||||
label: function() {
|
label: function() {
|
||||||
return "http"
|
return this.name||"http";
|
||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return "";
|
return this.name?"node_label_italic":"";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="http request">
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-method"><i class="icon-tasks"></i> Method</label>
|
||||||
|
<select type="text" id="node-input-method" style="width: 150px;">
|
||||||
|
<option value="GET">GET</option>
|
||||||
|
<option value="POST">POST</option>
|
||||||
|
<option value="PUT">PUT</option>
|
||||||
|
<option value="DELETE">DELETE</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-url"><i class="icon-tasks"></i> URL</label>
|
||||||
|
<input type="text" id="node-input-url" placeholder="http://">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/x-red" data-help-name="http request">
|
||||||
|
<p>Provides a node for making http requests.</p>
|
||||||
|
<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>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>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
The output message contains the following properties:
|
||||||
|
<ul>
|
||||||
|
<li><code>payload</code> is the body of the response</li>
|
||||||
|
<li><code>statusCode</code> is the status code of the response, or the error code if the request could not be completed</li>
|
||||||
|
<li><code>headers</code> is an object containing the response headers</li>
|
||||||
|
</ul>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('http request',{
|
||||||
|
category: 'function',
|
||||||
|
color:"rgb(231, 231, 174)",
|
||||||
|
defaults: {
|
||||||
|
name: {value:""},
|
||||||
|
method:{value:"GET"},
|
||||||
|
url:{value:""}
|
||||||
|
},
|
||||||
|
inputs:1,
|
||||||
|
outputs:1,
|
||||||
|
align: "right",
|
||||||
|
icon: "white-globe.png",
|
||||||
|
label: function() {
|
||||||
|
return this.name||"http request";
|
||||||
|
},
|
||||||
|
labelStyle: function() {
|
||||||
|
return this.name?"node_label_italic":"";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
var RED = require("../../red/red");
|
var RED = require("../../red/red");
|
||||||
var util = require("util");
|
var util = require("util");
|
||||||
|
var http = require("http");
|
||||||
|
var https = require("https");
|
||||||
|
var urllib = require("url");
|
||||||
|
var bodyParser = require("express").bodyParser();
|
||||||
|
|
||||||
function HTTPIn(n) {
|
function HTTPIn(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
@ -24,14 +28,15 @@ function HTTPIn(n) {
|
|||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
this.callback = function(req,res) {
|
this.callback = function(req,res) {
|
||||||
|
console.log(arguments.length);
|
||||||
node.send({req:req,res:res});
|
node.send({req:req,res:res});
|
||||||
}
|
}
|
||||||
if (this.method == "get") {
|
if (this.method == "get") {
|
||||||
RED.app.get(this.url,this.callback);
|
RED.app.get(this.url,this.callback);
|
||||||
} else if (this.method == "post") {
|
} else if (this.method == "post") {
|
||||||
RED.app.post(this.url,this.callback);
|
RED.app.post(this.url,bodyParser,this.callback);
|
||||||
} else if (this.method == "put") {
|
} else if (this.method == "put") {
|
||||||
RED.app.put(this.url,this.callback);
|
RED.app.put(this.url,bodyParser,this.callback);
|
||||||
} else if (this.method == "delete") {
|
} else if (this.method == "delete") {
|
||||||
RED.app.delete(this.url,this.callback);
|
RED.app.delete(this.url,this.callback);
|
||||||
}
|
}
|
||||||
@ -41,7 +46,7 @@ function HTTPIn(n) {
|
|||||||
for (var i in routes) {
|
for (var i in routes) {
|
||||||
if (routes[i].path == this.url) {
|
if (routes[i].path == this.url) {
|
||||||
routes.splice(i,1);
|
routes.splice(i,1);
|
||||||
break;
|
//break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -58,10 +63,59 @@ function HTTPOut(n) {
|
|||||||
if (msg.headers) {
|
if (msg.headers) {
|
||||||
res.set(msg.headers);
|
res.set(msg.headers);
|
||||||
}
|
}
|
||||||
var rc = msg.rc || 200;
|
var statusCode = msg.statusCode || 200;
|
||||||
msg.res.send(rc,msg.payload);
|
msg.res.send(statusCode,msg.payload);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("http response",HTTPOut);
|
RED.nodes.registerType("http response",HTTPOut);
|
||||||
|
|
||||||
|
function HTTPRequest(n) {
|
||||||
|
RED.nodes.createNode(this,n);
|
||||||
|
var url = n.url;
|
||||||
|
var method = n.method || "GET";
|
||||||
|
var httplib = (/^https/.test(url))?https:http;
|
||||||
|
var node = this;
|
||||||
|
this.on("input",function(msg) {
|
||||||
|
|
||||||
|
var opts = urllib.parse(msg.url||url);
|
||||||
|
opts.method = msg.method||method;
|
||||||
|
if (msg.headers) {
|
||||||
|
opts.header = headers;
|
||||||
|
}
|
||||||
|
var req = httplib.request(opts,function(res) {
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
var message = {
|
||||||
|
statusCode: res.statusCode,
|
||||||
|
headers: res.headers,
|
||||||
|
payload: ""
|
||||||
|
};
|
||||||
|
res.on('data',function(chunk) {
|
||||||
|
message.payload += chunk;
|
||||||
|
});
|
||||||
|
res.on('end',function() {
|
||||||
|
node.send(message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
req.on('error',function(err) {
|
||||||
|
msg.payload = err.toString();
|
||||||
|
msg.statusCode = err.code;
|
||||||
|
node.send(msg);
|
||||||
|
});
|
||||||
|
if (msg.payload) {
|
||||||
|
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
|
||||||
|
req.write(msg.payload);
|
||||||
|
} else if (typeof msg.payload == "number") {
|
||||||
|
req.write(msg.payload+"");
|
||||||
|
} else {
|
||||||
|
req.write(JSON.stringify(msg.payload));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req.end();
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
RED.nodes.registerType("http request",HTTPRequest);
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<script type="text/x-red" data-template-name="httpget">
|
<script type="text/x-red" data-template-name="httpget">
|
||||||
|
<div class="form-tips"><b>Deprecated</b>: please use the <i>http request</i> node.</div>
|
||||||
|
<br>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-baseurl"><i class="icon-tasks"></i> Base URL</label>
|
<label for="node-input-baseurl"><i class="icon-tasks"></i> Base URL</label>
|
||||||
<input type="text" id="node-input-baseurl" placeholder="http(s)://url">
|
<input type="text" id="node-input-baseurl" placeholder="http(s)://url">
|
||||||
|
Loading…
Reference in New Issue
Block a user