lib/requests/sendRequest.js
function sendRequest(method, path, config, data, additionalHeaders, responseParams, successCallback, errorCallback, isBatch, isAsync) {

    additionalHeaders = additionalHeaders || {};
    responseParams = responseParams || {};

    //add response parameters to parse
    responseParseParams.push(responseParams);

    //stringify passed data
    var stringifiedData = stringifyData(data, config);

    if (isBatch) {
        batchRequestCollection.push({
            method: method, path: path, config: config, data: stringifiedData, headers: additionalHeaders
        });
        return;
    }

    if (path === '$batch') {
        var batchResult = BatchConverter.convertToBatch(batchRequestCollection);

        stringifiedData = batchResult.body;

        //clear an array of requests
        batchRequestCollection.length = 0;

        additionalHeaders = setStandardHeaders(additionalHeaders);
        additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchResult.boundary;
    }
    else {
        additionalHeaders = setStandardHeaders(additionalHeaders);
    }

    responseParams.convertedToBatch = false;

    //if the URL contains more characters than max possible limit, convert the request to a batch request
    if (path.length > 2000) {
        var batchBoundary = 'dwa_batch_' + Utility.generateUUID();

        var batchBody = [];
        batchBody.push('--' + batchBoundary);
        batchBody.push('Content-Type: application/http');
        batchBody.push('Content-Transfer-Encoding: binary\n');
        batchBody.push(method + ' ' + config.webApiUrl + path + ' HTTP/1.1');

        for (var key in additionalHeaders) {
            if (key === 'Authorization')
                continue;

            batchBody.push(key + ': ' + additionalHeaders[key]);

            //authorization header is an exception. bug #27
            delete additionalHeaders[key];
        }

        batchBody.push('\n--' + batchBoundary + '--');

        stringifiedData = batchBody.join('\n');

        additionalHeaders = setStandardHeaders(additionalHeaders);
        additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchBoundary;
        path = '$batch';
        method = 'POST';

        responseParams.convertedToBatch = true;
    }

    if (config.impersonate && !additionalHeaders['MSCRMCallerID']) {
        additionalHeaders['MSCRMCallerID'] = config.impersonate;
    }

    var executeRequest;
    /* develblock:start */
    if (typeof XMLHttpRequest !== 'undefined') {
        /* develblock:end */
        executeRequest = require('./xhr');
        /* develblock:start */
    }
    else if (typeof process !== 'undefined') {
        executeRequest = require('./http');
    }
    /* develblock:end */

    var sendInternalRequest = function (token) {
        if (token) {
            if (!additionalHeaders) {
                additionalHeaders = {};
            }
            additionalHeaders['Authorization'] = 'Bearer ' +
                (token.hasOwnProperty('accessToken') ?
                    token.accessToken :
                    token);
        }

        executeRequest({
            method: method,
            uri: config.webApiUrl + path,
            data: stringifiedData,
            additionalHeaders: additionalHeaders,
            responseParams: responseParseParams,
            successCallback: successCallback,
            errorCallback: errorCallback,
            isAsync: isAsync,
            timeout: config.timeout
        });
    };

    //call a token refresh callback only if it is set and there is no "Authorization" header set yet
    if (config.onTokenRefresh && (!additionalHeaders || (additionalHeaders && !additionalHeaders['Authorization']))) {
        config.onTokenRefresh(sendInternalRequest);
    }
    else {
        sendInternalRequest();
    }
}
function sendRequest(method, path, config, data, additionalHeaders, responseParams, successCallback, errorCallback, isBatch, isAsync) {

    additionalHeaders = additionalHeaders || {};
    responseParams = responseParams || {};

    //add response parameters to parse
    responseParseParams.push(responseParams);

    //stringify passed data
    var stringifiedData = stringifyData(data, config);

    if (isBatch) {
        batchRequestCollection.push({
            method: method, path: path, config: config, data: stringifiedData, headers: additionalHeaders
        });
        return;
    }

    if (path === '$batch') {
        var batchResult = BatchConverter.convertToBatch(batchRequestCollection);

        stringifiedData = batchResult.body;

        //clear an array of requests
        batchRequestCollection.length = 0;

        additionalHeaders = setStandardHeaders(additionalHeaders);
        additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchResult.boundary;
    }
    else {
        additionalHeaders = setStandardHeaders(additionalHeaders);
    }

    responseParams.convertedToBatch = false;

    //if the URL contains more characters than max possible limit, convert the request to a batch request
    if (path.length > 2000) {
        var batchBoundary = 'dwa_batch_' + Utility.generateUUID();

        var batchBody = [];
        batchBody.push('--' + batchBoundary);
        batchBody.push('Content-Type: application/http');
        batchBody.push('Content-Transfer-Encoding: binary\n');
        batchBody.push(method + ' ' + config.webApiUrl + path + ' HTTP/1.1');

        for (var key in additionalHeaders) {
            if (key === 'Authorization')
                continue;

            batchBody.push(key + ': ' + additionalHeaders[key]);

            //authorization header is an exception. bug #27
            delete additionalHeaders[key];
        }

        batchBody.push('\n--' + batchBoundary + '--');

        stringifiedData = batchBody.join('\n');

        additionalHeaders = setStandardHeaders(additionalHeaders);
        additionalHeaders['Content-Type'] = 'multipart/mixed;boundary=' + batchBoundary;
        path = '$batch';
        method = 'POST';

        responseParams.convertedToBatch = true;
    }

    if (config.impersonate && !additionalHeaders['MSCRMCallerID']) {
        additionalHeaders['MSCRMCallerID'] = config.impersonate;
    }

    var executeRequest;
    /* develblock:start */
    if (typeof XMLHttpRequest !== 'undefined') {
        /* develblock:end */
        executeRequest = require('./xhr');
        /* develblock:start */
    }
    else if (typeof process !== 'undefined') {
        executeRequest = require('./http');
    }
    /* develblock:end */

    var sendInternalRequest = function (token) {
        if (token) {
            if (!additionalHeaders) {
                additionalHeaders = {};
            }
            additionalHeaders['Authorization'] = 'Bearer ' +
                (token.hasOwnProperty('accessToken') ?
                    token.accessToken :
                    token);
        }

        executeRequest({
            method: method,
            uri: config.webApiUrl + path,
            data: stringifiedData,
            additionalHeaders: additionalHeaders,
            responseParams: responseParseParams,
            successCallback: successCallback,
            errorCallback: errorCallback,
            isAsync: isAsync,
            timeout: config.timeout
        });
    };

    //call a token refresh callback only if it is set and there is no "Authorization" header set yet
    if (config.onTokenRefresh && (!additionalHeaders || (additionalHeaders && !additionalHeaders['Authorization']))) {
        config.onTokenRefresh(sendInternalRequest);
    }
    else {
        sendInternalRequest();
    }
}
function sendRequest ( method , path , config , data , additionalHeaders , responseParams , successCallback , errorCallback , isBatch , isAsync ) { additionalHeaders = additionalHeaders || { } ; responseParams = responseParams || { } ; //add response parameters to parse responseParseParams . push ( responseParams ) ; //stringify passed data var stringifiedData = stringifyData ( data , config ) ; if ( isBatch ) { batchRequestCollection . push ( { method : method , path : path , config : config , data : stringifiedData , headers : additionalHeaders } ) ; return ; } if ( path === '$batch' ) { var batchResult = BatchConverter . convertToBatch ( batchRequestCollection ) ; stringifiedData = batchResult . body ; //clear an array of requests batchRequestCollection . length = 0 ; additionalHeaders = setStandardHeaders ( additionalHeaders ) ; additionalHeaders [ 'Content-Type' ] = 'multipart/mixed;boundary=' + batchResult . boundary ; } else { additionalHeaders = setStandardHeaders ( additionalHeaders ) ; } responseParams . convertedToBatch = false ; //if the URL contains more characters than max possible limit, convert the request to a batch request if ( path . length > 2000 ) { var batchBoundary = 'dwa_batch_' + Utility . generateUUID ( ) ; var batchBody = [ ] ; batchBody . push ( '--' + batchBoundary ) ; batchBody . push ( 'Content-Type: application/http' ) ; batchBody . push ( 'Content-Transfer-Encoding: binary\n' ) ; batchBody . push ( method + ' ' + config . webApiUrl + path + ' HTTP/1.1' ) ; for ( var key in additionalHeaders ) { if ( key === 'Authorization' ) continue ; batchBody . push ( key + ': ' + additionalHeaders [ key ] ) ; //authorization header is an exception. bug #27 delete additionalHeaders [ key ] ; } batchBody . push ( '\n--' + batchBoundary + '--' ) ; stringifiedData = batchBody . join ( '\n' ) ; additionalHeaders = setStandardHeaders ( additionalHeaders ) ; additionalHeaders [ 'Content-Type' ] = 'multipart/mixed;boundary=' + batchBoundary ; path = '$batch' ; method = 'POST' ; responseParams . convertedToBatch = true ; } if ( config . impersonate && ! additionalHeaders [ 'MSCRMCallerID' ] ) { additionalHeaders [ 'MSCRMCallerID' ] = config . impersonate ; } var executeRequest ; /* develblock:start */ if ( typeof XMLHttpRequest !== 'undefined' ) { /* develblock:end */ executeRequest = require ( './xhr' ) ; /* develblock:start */ } else if ( typeof process !== 'undefined' ) { executeRequest = require ( './http' ) ; } /* develblock:end */ var sendInternalRequest = function ( token ) { if ( token ) { if ( ! additionalHeaders ) { additionalHeaders = { } ; } additionalHeaders [ 'Authorization' ] = 'Bearer ' + ( token . hasOwnProperty ( 'accessToken' ) ? token . accessToken : token ) ; } executeRequest ( { method : method , uri : config . webApiUrl + path , data : stringifiedData , additionalHeaders : additionalHeaders , responseParams : responseParseParams , successCallback : successCallback , errorCallback : errorCallback , isAsync : isAsync , timeout : config . timeout } ) ; } ; //call a token refresh callback only if it is set and there is no "Authorization" header set yet if ( config . onTokenRefresh && ( ! additionalHeaders || ( additionalHeaders && ! additionalHeaders [ 'Authorization' ] ) ) ) { config . onTokenRefresh ( sendInternalRequest ) ; } else { sendInternalRequest ( ) ; } }
Sends a request to given URL with given parameters

@param {string} method - Method of the request.
@param {string} path - Request path.
@param {Object} config - DynamicsWebApi config.
@param {Object} [data] - Data to send in the request.
@param {Object} [additionalHeaders] - Object with additional headers. IMPORTANT! This object does not contain default headers needed for every request.
@param {any} [responseParams] - parameters for parsing the response
@param {Function} successCallback - A callback called on success of the request.
@param {Function} errorCallback - A callback called when a request failed.
@param {boolean} [isBatch] - Indicates whether the request is a Batch request or not. Default: false
@param {boolean} [isAsync] - Indicates whether the request should be made synchronously or asynchronously.
Sends a request to given URL with given parameters