forked from microsoftgraph/nodejs-connect-rest-sample
/
requestUtil.js
85 lines (78 loc) · 2.25 KB
/
requestUtil.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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
var https = require('https');
/**
* Generates a GET request to the specified host
* @param {string} host the host to whom this request will be sent
* @param {string} path the path, relative to the host, to which this request will be sent
* @param {string} token the authorization token with which the request should be signed
* @param {callback} callback
*/
function getJson(host, path, token, callback) {
var options = {
host: host,
path: path,
method: 'GET',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer ' + token
}
};
https.get(options, function (response) {
var body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
callback(body);
});
response.on('error', function (e) {
console.error(e);
callback(null);
});
});
}
/**
* Generates a POST request (of Content-type ```application/json```)
* @param {string} host the host to whom this request will be sent
* @param {string} path the path, relative to the host, to which this request will be sent
* @param {string} token the authorization token with which the request should be signed
* @param {string} data the data which will be 'POST'ed
* @param {callback} callback
*/
function postData(host, path, token, data, callback) {
var outHeaders = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
'Content-Length': data.length
};
var options = {
host: host,
path: path,
method: 'POST',
headers: outHeaders
};
// Set up the request
var post = https.request(options, function (res) {
console.log(res.statusCode);
console.log(res.statusMessage);
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
res.on('end', function () {
callback(res);
});
});
// write the outbound data to it
post.write(data);
// we're done!
post.end();
post.on('error', function (e) {
console.log('Request error: ' + e.message);
});
}
exports.getJson = getJson;
exports.postData = postData;