* @ngdoc service * @kind function * @name $http * @requires ng.$httpBackend * @requires $cacheFactory * @requires $rootScope * @requires $q * @requires $injector * * @description * The `$http` service is a core AngularJS service that facilitates comm
(requestConfig)
| 12337 | </example> |
| 12338 | */ |
| 12339 | function $http(requestConfig) { |
| 12340 | |
| 12341 | if (!isObject(requestConfig)) { |
| 12342 | throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig); |
| 12343 | } |
| 12344 | |
| 12345 | if (!isString($sce.valueOf(requestConfig.url))) { |
| 12346 | throw minErr('$http')('badreq', 'Http request configuration url must be a string or a $sce trusted object. Received: {0}', requestConfig.url); |
| 12347 | } |
| 12348 | |
| 12349 | var config = extend({ |
| 12350 | method: 'get', |
| 12351 | transformRequest: defaults.transformRequest, |
| 12352 | transformResponse: defaults.transformResponse, |
| 12353 | paramSerializer: defaults.paramSerializer, |
| 12354 | jsonpCallbackParam: defaults.jsonpCallbackParam |
| 12355 | }, requestConfig); |
| 12356 | |
| 12357 | config.headers = mergeHeaders(requestConfig); |
| 12358 | config.method = uppercase(config.method); |
| 12359 | config.paramSerializer = isString(config.paramSerializer) ? |
| 12360 | $injector.get(config.paramSerializer) : config.paramSerializer; |
| 12361 | |
| 12362 | $browser.$$incOutstandingRequestCount(); |
| 12363 | |
| 12364 | var requestInterceptors = []; |
| 12365 | var responseInterceptors = []; |
| 12366 | var promise = $q.resolve(config); |
| 12367 | |
| 12368 | // apply interceptors |
| 12369 | forEach(reversedInterceptors, function(interceptor) { |
| 12370 | if (interceptor.request || interceptor.requestError) { |
| 12371 | requestInterceptors.unshift(interceptor.request, interceptor.requestError); |
| 12372 | } |
| 12373 | if (interceptor.response || interceptor.responseError) { |
| 12374 | responseInterceptors.push(interceptor.response, interceptor.responseError); |
| 12375 | } |
| 12376 | }); |
| 12377 | |
| 12378 | promise = chainInterceptors(promise, requestInterceptors); |
| 12379 | promise = promise.then(serverRequest); |
| 12380 | promise = chainInterceptors(promise, responseInterceptors); |
| 12381 | promise = promise.finally(completeOutstandingRequest); |
| 12382 | |
| 12383 | return promise; |
| 12384 | |
| 12385 | |
| 12386 | function chainInterceptors(promise, interceptors) { |
| 12387 | for (var i = 0, ii = interceptors.length; i < ii;) { |
| 12388 | var thenFn = interceptors[i++]; |
| 12389 | var rejectFn = interceptors[i++]; |
| 12390 | |
| 12391 | promise = promise.then(thenFn, rejectFn); |
| 12392 | } |
| 12393 | |
| 12394 | interceptors.length = 0; |
| 12395 | |
| 12396 | return promise; |
no test coverage detected