forked from IdanZel/node-red-contrib-http-ntlm-req
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-ntlm-req.js
86 lines (75 loc) · 2.35 KB
/
http-ntlm-req.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module.exports = function (RED) {
function HttpNtlmReqNode(config) {
var httpntlm = require('httpntlm');
RED.nodes.createNode(this, config);
const node = this;
const resetStatus = () => node.status({});
const raiseError = (text, msg) => {
node.status({ fill: "red", shape: "dot", text: text });
node.error(text, msg);
};
node.name = config.name;
node.url = config.url;
node.method = config.method;
node.authconf = RED.nodes.getNode(config.auth);
resetStatus();
node.on('input', function (msg) {
const requestCallback = (err, res) => {
resetStatus();
if (res !== undefined && res.body !== undefined) {
//msg.payload = JSON.parse(res.body);
msg.payload = res.body;
if(res.statusCode>=400) {
raiseError('Response from server: ' + res.statusCode, msg);
}
} else {
raiseError(err.message, msg);
}
node.send(msg);
};
var params = (typeof msg.params==='undefined')?"":msg.params;
var url = (typeof msg.url==='undefined')?node.url:msg.url;
const connData = {
username: node.authconf.user,
password: node.authconf.pass,
domain: node.authconf.doman,
workstation: '',
headers: (typeof msg.headers==='undefined')?{}:msg.headers
};
switch(parseInt(node.method)){
case 0: // GET
{
connData.url = url + params;
httpntlm.get(connData, requestCallback);
break;
}
case 1: // POST
{
connData.url = url + params;
if(msg.payload!==undefined){
connData.body = JSON.stringify(msg.payload);
connData.headers['Content-Type'] = (typeof connData.headers['Content-Type']==='undefined')?'application/json':connData.headers['Content-Type'];
}
httpntlm.post(connData, requestCallback);
break;
}
case 2: // PUT
{
connData.url = url + params;
if(msg.payload!==undefined){
connData.body = JSON.stringify(msg.payload);
connData.headers['Content-Type'] = (typeof connData.headers['Content-Type']==='undefined')?'application/json':connData.headers['Content-Type'];
}
httpntlm.put(connData, requestCallback);
break;
}
default:
{
raiseError('No method defined!', msg);
break;
}
}
});
}
RED.nodes.registerType("http-ntlm-req", HttpNtlmReqNode);
}