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

Add mustache template support to the HTTP Request url

This commit is contained in:
Nick O'Leary 2014-04-15 22:49:39 +01:00
parent e9c6501771
commit d5b36fcadc
2 changed files with 70 additions and 64 deletions

View File

@ -64,46 +64,6 @@
</script> </script>
<script type="text/javascript">
RED.nodes.registerType('http in',{
category: 'input',
color:"rgb(231, 231, 174)",
defaults: {
name: {value:""},
url: {value:"",required:true},
method: {value:"get",required:true}
},
inputs:0,
outputs:1,
icon: "white-globe.png",
label: function() {
if (this.name) {
return this.name;
} else if (this.url) {
var root = RED.settings.httpNodeRoot.slice(0,-1);
root += this.url;
return "["+this.method+"] "+root;
} else {
return "http";
}
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
var root = RED.settings.httpNodeRoot.slice(0,-1);
if (root == "") {
$("#node-input-tip").hide();
} else {
$("#node-input-path").html(root);
$("#node-input-tip").show();
}
//document.getElementById("node-config-wsdocpath").innerHTML=
}
});
</script>
<script type="text/x-red" data-template-name="http response"> <script type="text/x-red" data-template-name="http response">
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
@ -123,29 +83,6 @@
</ul> </ul>
</script> </script>
<script type="text/javascript">
RED.nodes.registerType('http response',{
category: 'output',
color:"rgb(231, 231, 174)",
defaults: {
name: {value:""}
},
inputs:1,
outputs:0,
align: "right",
icon: "white-globe.png",
label: function() {
return this.name||"http";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>
<script type="text/x-red" data-template-name="http request"> <script type="text/x-red" data-template-name="http request">
<div class="form-row"> <div class="form-row">
<label for="node-input-method"><i class="icon-tasks"></i> Method</label> <label for="node-input-method"><i class="icon-tasks"></i> Method</label>
@ -190,7 +127,10 @@
pairs to be added as request headers</li> pairs to be added as request headers</li>
<li><code>payload</code> is sent as the body of the request</li> <li><code>payload</code> is sent as the body of the request</li>
</ul> </ul>
<p>When configured within the node, the URL property can contain <a href="http://mustache.github.io/mustache.5.html" target="_new">mustache-style</a> tags. These allow the
url to be constructed using values of the incoming message. For example, if the url is set to
<code>example.com/{{topic}}</code>, it will have the value of <code>msg.topic</code> automatically inserted.</p>
<p>
The output message contains the following properties: The output message contains the following properties:
<ul> <ul>
<li><code>payload</code> is the body of the response</li> <li><code>payload</code> is the body of the response</li>
@ -200,6 +140,62 @@
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('http in',{
category: 'input',
color:"rgb(231, 231, 174)",
defaults: {
name: {value:""},
url: {value:"",required:true},
method: {value:"get",required:true}
},
inputs:0,
outputs:1,
icon: "white-globe.png",
label: function() {
if (this.name) {
return this.name;
} else if (this.url) {
var root = RED.settings.httpNodeRoot.slice(0,-1);
root += this.url;
return "["+this.method+"] "+root;
} else {
return "http";
}
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
var root = RED.settings.httpNodeRoot.slice(0,-1);
if (root == "") {
$("#node-input-tip").hide();
} else {
$("#node-input-path").html(root);
$("#node-input-tip").show();
}
//document.getElementById("node-config-wsdocpath").innerHTML=
}
});
RED.nodes.registerType('http response',{
category: 'output',
color:"rgb(231, 231, 174)",
defaults: {
name: {value:""}
},
inputs:1,
outputs:0,
align: "right",
icon: "white-globe.png",
label: function() {
return this.name||"http";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
RED.nodes.registerType('http request',{ RED.nodes.registerType('http request',{
category: 'function', category: 'function',
color:"rgb(231, 231, 174)", color:"rgb(231, 231, 174)",

View File

@ -21,6 +21,8 @@ var https = require("follow-redirects").https;
var urllib = require("url"); var urllib = require("url");
var express = require("express"); var express = require("express");
var getBody = require('raw-body'); var getBody = require('raw-body');
var mustache = require("mustache");
var cors = require('cors'); var cors = require('cors');
var jsonParser = express.json(); var jsonParser = express.json();
var urlencParser = express.urlencoded(); var urlencParser = express.urlencoded();
@ -131,10 +133,18 @@ RED.nodes.registerType("http response",HTTPOut);
function HTTPRequest(n) { function HTTPRequest(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var nodeUrl = n.url; var nodeUrl = n.url;
var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
var nodeMethod = n.method || "GET"; var nodeMethod = n.method || "GET";
var node = this; var node = this;
var credentials = RED.nodes.getCredentials(n.id); var credentials = RED.nodes.getCredentials(n.id);
this.on("input",function(msg) { this.on("input",function(msg) {
if (msg.url) {
url = msg.url;
} else if (isTemplatedUrl) {
url = mustache.render(nodeUrl,msg);
} else {
url = nodeUrl;
}
var url = msg.url||nodeUrl; var url = msg.url||nodeUrl;
var method = (msg.method||nodeMethod).toUpperCase(); var method = (msg.method||nodeMethod).toUpperCase();
var opts = urllib.parse(url); var opts = urllib.parse(url);