function sendRequest(path, method, data, useToken, callback){
    var headers = {}
    headers['Content-Type'] = 'application/json';
    headers['Content-Length'] =  Buffer.byteLength(JSON.stringify(data));

    if (useToken && TOKEN.length) {
        headers['Authorization'] = 'Bearer '+TOKEN;
    }
    if (method === undefined) {
        method = 'POST';
    }
    if (useToken === undefined) {
        useToken = false;
    }

    var options = {
        //uri: API_URL,
        path: '/'+path,
        port: 443,
        hostname: API_URL,
        method: method,
        headers: headers,
    };

    var req = https.request(
        options,
        function(response) {
            var str = '';
            response.on('data', function (chunk) {
                if (response.statusCode==401) {
                    getToken();
                    sendRequest(path, method, data, true, callback);
                } else {
                    str += chunk;
                }
            });

            response.on('end', function () {
                if (response.statusCode != 401) {
                    try {
                        var answer = JSON.parse(str);
                    } catch (ex) {
                        var answer = returnError();
                    }
                    callback(answer);
                }
            });
        }
    );
    req.write(JSON.stringify(data));
    req.end();
}
function sendRequest(path, method, data, useToken, callback){
    var headers = {}
    headers['Content-Type'] = 'application/json';
    headers['Content-Length'] =  Buffer.byteLength(JSON.stringify(data));

    if (useToken && TOKEN.length) {
        headers['Authorization'] = 'Bearer '+TOKEN;
    }
    if (method === undefined) {
        method = 'POST';
    }
    if (useToken === undefined) {
        useToken = false;
    }

    var options = {
        //uri: API_URL,
        path: '/'+path,
        port: 443,
        hostname: API_URL,
        method: method,
        headers: headers,
    };

    var req = https.request(
        options,
        function(response) {
            var str = '';
            response.on('data', function (chunk) {
                if (response.statusCode==401) {
                    getToken();
                    sendRequest(path, method, data, true, callback);
                } else {
                    str += chunk;
                }
            });

            response.on('end', function () {
                if (response.statusCode != 401) {
                    try {
                        var answer = JSON.parse(str);
                    } catch (ex) {
                        var answer = returnError();
                    }
                    callback(answer);
                }
            });
        }
    );
    req.write(JSON.stringify(data));
    req.end();
}
function sendRequest ( path , method , data , useToken , callback ) { var headers = { } headers [ 'Content-Type' ] = 'application/json' ; headers [ 'Content-Length' ] = Buffer . byteLength ( JSON . stringify ( data ) ) ; if ( useToken && TOKEN . length ) { headers [ 'Authorization' ] = 'Bearer ' + TOKEN ; } if ( method === undefined ) { method = 'POST' ; } if ( useToken === undefined ) { useToken = false ; } var options = { //uri: API_URL, path : '/' + path , port : 443 , hostname : API_URL , method : method , headers : headers , } ; var req = https . request ( options , function ( response ) { var str = '' ; response . on ( 'data' , function ( chunk ) { if ( response . statusCode == 401 ) { getToken ( ) ; sendRequest ( path , method , data , true , callback ) ; } else { str += chunk ; } } ) ; response . on ( 'end' , function ( ) { if ( response . statusCode != 401 ) { try { var answer = JSON . parse ( str ) ; } catch ( ex ) { var answer = returnError ( ) ; } callback ( answer ) ; } } ) ; } ) ; req . write ( JSON . stringify ( data ) ) ; req . end ( ) ; }
Form and send request to API service

@param path
@param method
@param data
@param useToken
@param callback
Define the function  that will be called
when a response is received.
Form and send request to API service