-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.js
48 lines (37 loc) · 1.11 KB
/
http.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
'use strict'
var request = require('request');
var http = {};
http.post = function(url, headers, body) {
return new Promise(function(resolve, reject) {
var options = {
url: url,
headers: headers,
body: body
};
function callback(error, response, body) {
if (!error && (response.statusCode == 200 || response.statusCode == 201)) {
resolve(JSON.parse(body));
} else {
reject(body);
}
}
request.post(options, callback);
});
}
http.get = function(url, headers) {
return new Promise(function(resolve, reject) {
var options = {
url: url,
headers: headers
};
function callback(error, response, body) {
if (!error && (response.statusCode == 200 || response.statusCode == 201)) {
resolve(JSON.parse(body));
} else {
reject(body);
}
}
request.get(options, callback);
});
}
module.exports = http;