-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.js
executable file
·41 lines (35 loc) · 964 Bytes
/
request.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
const { stringify } = require('querystring');
const { Agent, request } = require('https');
/**
* Send an HTTPS request and receive a response
*/
module.exports = function httpsJeedom(opts, queryParams) {
return new Promise((resolve, reject) => {
var postData = stringify(queryParams);
opts.method = 'POST';
opts.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
};
opts.agent = new Agent({keepAlive: true});
const req = request(opts, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
if (res.statusCode !== 200)
return reject(new Error(`Unsuccessful request [${res.statusCode}]`));
return (opts.json) ? resolve(JSON.parse(body)) : resolve(body);
});
res.on('error', (err) => {
return reject(err);
});
});
req.on('error', (err) => {
return reject(err);
});
req.write(postData);
req.end();
});
};