-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax.js
More file actions
88 lines (76 loc) · 2.31 KB
/
ajax.js
File metadata and controls
88 lines (76 loc) · 2.31 KB
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
var http = require('http');
var config = require("./config");
var client = http.createClient(80, config.cfhost);
module.exports = {
jsonPost: function(args) {
var dataBody = args.data ? JSON.stringify(args.data) : "";
var headers = {
'host': config.cfhost,
'Content-Type': 'application/json',
'Accept': "application/json",
'Content-Length': dataBody.length
};
if (args.token) {
headers["AUTHORIZATION"] = args.token;
}
if (args.proxy) {
headers["PROXY-USER"] = args.proxy;
}
var request = client.request(args.cmd || 'POST', args.path, headers);
request.on('response', function(response) {
var jsonData = "";
response.on('data', function(chunk) {
jsonData += chunk.toString();
});
response.on('error', function(err) {
args.onResponse(err);
});
response.on('end', function() {
var msgHash;
try {
msgHash = JSON.parse(jsonData);
}
catch (err) {
msgHash = jsonData;
}
args.onResponse(null, {"data": msgHash, "status": response.statusCode});
});
});
if (dataBody) {
request.write(dataBody);
}
request.end();
},
uploadBuffer: function(args) {
var contentLen = 0;
for (var i=0; i<args.bufferList.length; i++) {
contentLen += args.bufferList[i].length;
}
var headers = {
'host': config.cfhost,
'Content-Type': 'multipart/form-data; boundary=' + args.boundary,
'Content-Length': contentLen
};
if (args.token) {
headers["AUTHORIZATION"] = args.token;
}
if (args.proxy) {
headers["PROXY-USER"] = args.proxy;
}
var request = client.request('post', args.path, headers);
request.on('response', function(response) {
var jsonData;
response.on('error', function(err) {
args.onResponse(err);
});
response.on('end', function() {
console.log(response.statusCode);
args.onResponse(null, response.statusCode);
});
});
for (i=0; i<args.bufferList.length; i++) {
request.write(args.bufferList[i]);
}
request.end();
}
}