forked from easemob/emchat-server-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
92 lines (87 loc) · 3.13 KB
/
request.js
File metadata and controls
92 lines (87 loc) · 3.13 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
89
90
91
92
var https = require('https');
var config = require('./resources/config');
var fs = require('fs');
var fetch = require('node-fetch');
var host = config.host;
var org_name = config.org_name;
var app_name = config.app_name;
exports.httpRequest = function (json) {
json = json || {};
json.data = json.data || {};
json.method = json.method || 'GET';
json.headers = json.headers || {};
json.query = json.query || {};
var postData = JSON.stringify(json.data);
var ca = fs.readFileSync(config.ca, 'utf-8');
//request parameters
var options = {
host: host,
path: '/' + org_name + '/' + app_name + '/' + json.path,
method: json.method,
headers: json.headers,
ca: [ca],
agent: false
};
//connect with query parameters
if (json.query != null) {
options.path += '?';
for (var key in json.query) {
if (json.query[key] != null) {
options.path += key + '=' + json.query[key] + '&';
}
}
options.path = options.path.substring(0, options.path.length - 1);
}
//send request
var req = https.request(options, function (res) {
var chunks = '';
var size = 0;
res.setEncoding('utf8');
console.log('------------------------------request--------------------------------');
console.log('host: ' + options.host + '\n' + 'path: ' + options.path + '\n' + 'method: ' + options.method);
res.on('data', function (chunk) {
chunks += chunk;
size += chunk.length;
});
res.on('end', function () {
//get response data
//var data = JSON.parse(Buffer.concat(chunks, size).toString());
console.log('------------------------------response--------------------------------');
console.log('StatusCode: ' + res.statusCode);
if (typeof json.callback == 'function')
json.callback(chunks);
});
});
//print error message
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(postData);
req.end();
};
exports.uploadFile = function (json) {
json = json || {};
json.data = json.data || {};
json.method = json.method || 'POST';
json.headers = json.headers || {};
json.query = json.query || {};
json.form = json.form || {};
var ca = fs.readFileSync(config.ca, 'utf-8');
console.log('------------------------------request--------------------------------');
console.log('host: ' + host + '\n' + 'path: ' + json.path + '\n' + 'method: ' + json.method);
fetch('https://' + host + '/' + org_name + '/' + app_name + '/' + json.path, {
method: json.method,
body: json.form,
headers: json.headers,
ca: [ca]
}).then(function (res) {
console.log('------------------------------response--------------------------------');
console.log('StatusCode: ' + res.status);
return res.json();
}).then(function (json) {
console.log(json);
}).catch(function (err) {
console.log(err);
});
};