-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-request-text.js
124 lines (105 loc) · 3.45 KB
/
http-request-text.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// http-request-text @ npm
// Simply enclose node.js http.request() for responseText/responseJson
var http = require('http');
// methodOrOptions: string "POST"/"GET"/..., or user-defined options, ref. http.request().
// callback: function( Error:{ data.* }, data:{ responseText, statusCode, statusMessage, headers, userData } )
var requestText = function (url, methodOrOptions, postData, headers, callback, userData) {
//options
var options = (typeof methodOrOptions === "string") ? { method: methodOrOptions } : (methodOrOptions || {});
if (!options.headers) {
if (headers) options.headers = headers;
else if (options.method === "POST") options.headers =
{ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' };
}
//cleanup
var tmid;
var cleanup = () => {
if (tmid) { clearTimeout(tmid); tmid = null; };
callback = null; //call only once
}
//request
var req = http.request(url, options, function (res) {
var body = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
if (callback) {
var resData = {
responseText: body,
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers,
};
if (userData) resData.userData = userData;
if (res.statusCode === 200) {
callback(null, resData)
}
else {
callback(Object.assign(Error(res.statusCode + " " + res.statusMessage +
(body ? (", " + body.slice(0, 255)) : "")), resData));
}
}
cleanup();
});
if (options.timeout > 0) { //use options.options.timeout ( before connected ) as waiting timeout also ( after connected )
tmid = res.setTimeout(options.timeout, () => {
if (callback) {
var ret = Error("timeout, " + options.timeout);
if (userData) ret.userData = userData;
callback(ret);
}
tmid = null;
cleanup();
res.abort();
})
}
});
req.on('error', function (err) {
//console.log('request error: ' + err.message);
if (callback) {
if (userData) err.userData = userData;
callback(err);
}
cleanup();
});
if (postData) {
//console.log("postData: "+typeof(postData)+", ["+postData+"]");
req.write(postData);
}
req.end();
}
// callback: function( Error:{ data.* }, data:{ responseJson, data.* from requestText() } )
var requestJson = function (url, methodOrOptions, postData, headers, callback, userData) {
requestText(url, methodOrOptions, postData, headers, function (error, data) {
if (!error) {
try {
data.responseJson = JSON.parse(data.responseText);
}
catch (ex) {
console.log(ex);
error = Object.assign(Error("JSON parse error, " + ex.message), data);
}
}
callback && callback(error, data);
}, userData);
}
// callback: function( error, data:responseText )
var _text = function (url, methodOrOptions, postData, headers, callback, userData) {
requestText(url, methodOrOptions, postData, headers, function (error, data) {
callback && callback(error, error ? data : data.responseText);
}, userData);
}
// callback: function( error, data:responseJson )
var _json = function (url, methodOrOptions, postData, headers, callback, userData) {
requestJson(url, methodOrOptions, postData, headers, function (error, data) {
callback && callback(error, error ? data : data.responseJson);
}, userData);
}
//module
module.exports = exports = requestText;
exports.requestText = requestText;
exports.requestJson = requestJson;
exports.text = _text;
exports.json = _json;