-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmwrequest.js
58 lines (48 loc) · 1.36 KB
/
bmwrequest.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
var https = require("https");
exports.call = function(_host, _path, _postData, _callbackSuccess, _callbackError)
{
var hasToken = typeof(global.token) === "string" && global.token.length > 0;
var options =
{
hostname: _host,
port: '443',
path: _path,
method: _postData != null ? 'POST' : 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': _postData != null ? Buffer.byteLength(_postData) : 0
}
};
if(hasToken)
{
options.headers['Authorization'] = global.tokenType + " " + global.token;
}
console.log("Calling " + options.hostname + options.path);
const req = https.request(options, function(res)
{
if(res.statusCode >= 400 && res.statusCode < 600)
console.log("Request error for URL " + _path);
// console.log('STATUS: ' + );
// console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
var data = "";
res.on('data', function(chunk)
{
data += chunk;
});
res.on('end', function()
{
// console.log('BODY: ' + data);
// console.log('FOR PATH: ' + _path);
_callbackSuccess(data, res.headers);
});
});
req.on('error', function(e)
{
console.error('request error: ' + e.message);
_callbackError(e);
});
if(_postData != null)
req.write(_postData);
req.end();
};