-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhRequest.js
54 lines (48 loc) · 1.09 KB
/
hRequest.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
var hRequest = { }
var querystring = require('querystring');
var http = require('http');
var https = require('https');
var url = require('url');
var adehun = require('Adehun');
hRequest.post = function(href, params, headers){
var params = querystring.stringify(params);
var rString = '';
var opt = url.parse(href);
var proto;
if(opt.protocol == 'http:'){
proto = http;
} else if(opt.protocol == 'https:'){
proto = https;
}
opt.method = 'POST';
opt.headers = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : params.length,
}
if(headers !== undefined){
for(header in headers){
opt.headers[header] = headers[header];
}
}
var dProm = adehun.deferred();
var req = proto.request(opt, function(res){
res.setEncoding('utf8');
res.on('data', function(data){
rString += data;
});
res.on('end', function(){
dProm.resolve({
code : res.statusCode,
headers : res.headers,
answer : rString
});
});
});
req.on('error', function(e){
dProm.reject(e);
});
req.write(params);
req.end();
return dProm.promise;
}
module.exports = hRequest;