-
Notifications
You must be signed in to change notification settings - Fork 4
/
http_request.js
79 lines (75 loc) · 2.33 KB
/
http_request.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
var http = require('https');
function request(options, callback) {
var resStr = '';
// Set up the request
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var resStr = '';
res.on('data', function (chunk) {
resStr += chunk;
}).on('end', function () {
if(options.headers['Content-Type'] === 'application/json') {
var response = JSON.parse(resStr);
if(options.method === "DELETE" && response.found === true) {
response.deleted = true;
} else {
// do nothing ... istanbul!
}
callback(response);
}
else {
callback(resStr); // return response as object
}
})
}) // got a better idea? submit an issue! (lets do it!)
return req;
}
function options(record, method) {
var o = {
host: '127.0.0.1',
port: 9200,
path: '/',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
if(process.env.SEARCHBOX_SSL_URL) {
var url = process.env.SEARCHBOX_SSL_URL;
// e.g: "https://un:[email protected]"
var unpw = url.split('://')[1].split(':');
var un = unpw[0];
var pw = unpw[1].split('@')[0];
console.log(un +':'+pw);
var auth = (new Buffer(un + ':' + pw, 'utf8')).toString('base64');
o.headers['Authorization'] = 'Basic ' + auth;
o.host = url.split('@')[1];
o.port = 443;
}
console.log(' - - - - - - - - - - - - - - options:')
console.log(o);
console.log(' - - - - - - - - - - - - - - - - - - -')
return o;
}
process.env.SEARCHBOX_SSL_URL = 'https://un:[email protected]'
var opts = options();
var username = 'username';
var password = 'passsword'
var basic = 'Basic ' + (new Buffer(username + ':' + password, 'utf8')).toString('base64');
// var basic = 'Basic '+username+':'+password
// opts.headers.auth = basic;
// opts.headers['Authorization'] = basic;
console.log(opts);
request(opts, function(res){
console.log(res);
}).end();
console.log(' - - - - - - - - - - - - - - - - - ');
// http.get("http://un:[email protected]", function(res) {
// console.log("Got response: " + res.statusCode);
// // console.log(res);
// res.on("data", function(chunk) {
// console.log("BODY: " + chunk);
// });
// }).on('error', function(e) {
// console.log("Got error: " + e.message);
// });