-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpclient.js
41 lines (34 loc) · 874 Bytes
/
httpclient.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
var http = require("http");
var https = require("https");
var urllib = require("url");
var assert = require('assert');
function post(ckantsEndpoint, token, payload, cb) {
assert(ckantsEndpoint && token && payload);
var payload = JSON.stringify(payload);
var opts = urllib.parse(ckantsEndpoint);
opts.headers = {
'content-type': 'application/json',
'content-length': Buffer.byteLength(payload, 'utf8'),
'Authorization': token
};
opts.method = 'POST';
var req = ((/^https/.test(ckantsEndpoint)) ? https : http).request(opts, function (res) {
var result = '';
res.on('data', function (chunk) {
result += chunk;
});
res.on('end', function () {
cb(result);
});
});
req.on('error', function (err) {
cb(err);
});
if (payload) {
req.write(payload);
}
req.end();
}
module.exports = {
post: post
}