mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add Binary paylaod option to Http request node
Also add JSON parse option to output Allow user to select override using msg.method to stop getting warning. Fix to close #399
This commit is contained in:
parent
6fc3aab907
commit
27ef7d972f
@ -91,6 +91,7 @@
|
||||
<option value="POST">POST</option>
|
||||
<option value="PUT">PUT</option>
|
||||
<option value="DELETE">DELETE</option>
|
||||
<option value="use">- set by msg.method -</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
@ -100,7 +101,7 @@
|
||||
<div class="form-row">
|
||||
<label> </label>
|
||||
<input type="checkbox" id="node-input-useAuth" style="display: inline-block; width: auto; vertical-align: top;">
|
||||
<label for="node-input-useAuth" style="width: 70%;">Use basic authentication?</label>
|
||||
<label for="node-input-useAuth" style="width: 70%;">Use basic authentication ?</label>
|
||||
</div>
|
||||
<div class="form-row node-input-useAuth-row">
|
||||
<label for="node-input-user"><i class="fa fa-user"></i> Username</label>
|
||||
@ -110,10 +111,19 @@
|
||||
<label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
|
||||
<input type="password" id="node-input-password">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-ret"><i class="fa fa-arrow-left"></i> Return</label>
|
||||
<select type="text" id="node-input-ret" style="width:72%;">
|
||||
<option value="txt">a UTF-8 string</option>
|
||||
<option value="bin">a binary buffer</option>
|
||||
<option value="obj">a parsed JSON object</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-tips" id="tip-json" hidden>Tip: If the JSON parse fails the fetched string is returned as-is.</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="http request">
|
||||
@ -212,6 +222,7 @@
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
method:{value:"GET"},
|
||||
ret: {value:"txt"},
|
||||
url:{value:""},
|
||||
//user -> credentials
|
||||
//pass -> credentials
|
||||
@ -248,6 +259,14 @@
|
||||
$('#node-input-password').val('');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
$("#node-input-ret").change(function() {
|
||||
if ($("#node-input-ret").val() === "obj") {
|
||||
$("#tip-json").show();
|
||||
} else {
|
||||
$("#tip-json").hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
@ -23,7 +23,6 @@ module.exports = function(RED) {
|
||||
var getBody = require('raw-body');
|
||||
var mustache = require("mustache");
|
||||
var querystring = require("querystring");
|
||||
|
||||
var cors = require('cors');
|
||||
var jsonParser = express.json();
|
||||
var urlencParser = express.urlencoded();
|
||||
@ -150,6 +149,7 @@ module.exports = function(RED) {
|
||||
var nodeUrl = n.url;
|
||||
var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
|
||||
var nodeMethod = n.method || "GET";
|
||||
this.ret = n.ret || "txt";
|
||||
var node = this;
|
||||
this.on("input",function(msg) {
|
||||
node.status({fill:"blue",shape:"dot",text:"requesting"});
|
||||
@ -170,15 +170,19 @@ module.exports = function(RED) {
|
||||
}
|
||||
|
||||
var method;
|
||||
if (msg.method) {
|
||||
if (n.method && (n.method !== msg.method)) {
|
||||
node.warn("Deprecated: msg properties should not override set node properties. See bit.ly/nr-override-msg-props");
|
||||
if (msg.method) { // if method set in msg
|
||||
if (n.method && (n.method !== "use")) { // warn if override option not set
|
||||
node.warn("Deprecated: msg properties should not override fixed node properties. Use explicit override option. See bit.ly/nr-override-msg-props");
|
||||
}
|
||||
method = msg.method.toUpperCase();
|
||||
method = msg.method.toUpperCase(); // but use it anyway
|
||||
} else {
|
||||
method = nodeMethod.toUpperCase();
|
||||
if (n.method !== "use") {
|
||||
method = nodeMethod.toUpperCase(); // otherwise use the selected method
|
||||
} else { // unless they selected override
|
||||
method = "GET"; // - in which case default to GET
|
||||
}
|
||||
//node.log(method+" : "+url);
|
||||
}
|
||||
node.log(method+" : "+url);
|
||||
var opts = urllib.parse(url);
|
||||
opts.method = method;
|
||||
opts.headers = {};
|
||||
@ -221,7 +225,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
|
||||
var req = ((/^https/.test(url))?https:http).request(opts,function(res) {
|
||||
res.setEncoding('utf8');
|
||||
(node.ret === "bin") ? res.setEncoding('binary') : res.setEncoding('utf8');
|
||||
msg.statusCode = res.statusCode;
|
||||
msg.headers = res.headers;
|
||||
msg.payload = "";
|
||||
@ -229,6 +233,13 @@ module.exports = function(RED) {
|
||||
msg.payload += chunk;
|
||||
});
|
||||
res.on('end',function() {
|
||||
if (node.ret === "bin") {
|
||||
msg.payload = new Buffer(msg.payload,"binary");
|
||||
}
|
||||
else if (node.ret === "obj") {
|
||||
try { msg.payload = JSON.parse(msg.payload); }
|
||||
catch(e) { node.warn("JSON parse error"); }
|
||||
}
|
||||
node.send(msg);
|
||||
node.status({});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user