* @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)
| 12971 | </example> |
| 12972 | */ |
| 12973 | function $http(requestConfig) { |
| 12974 | |
| 12975 | if (!isObject(requestConfig)) { |
| 12976 | throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig); |
| 12977 | } |
| 12978 | |
| 12979 | if (!isString($sce.valueOf(requestConfig.url))) { |
| 12980 | throw minErr('$http')('badreq', 'Http request configuration url must be a string or a $sce trusted object. Received: {0}', requestConfig.url); |
| 12981 | } |
| 12982 | |
| 12983 | var config = extend({ |
| 12984 | method: 'get', |
| 12985 | transformRequest: defaults.transformRequest, |
| 12986 | transformResponse: defaults.transformResponse, |
| 12987 | paramSerializer: defaults.paramSerializer, |
| 12988 | jsonpCallbackParam: defaults.jsonpCallbackParam |
| 12989 | }, requestConfig); |
| 12990 | |
| 12991 | config.headers = mergeHeaders(requestConfig); |
| 12992 | config.method = uppercase(config.method); |
| 12993 | config.paramSerializer = isString(config.paramSerializer) ? |
| 12994 | $injector.get(config.paramSerializer) : config.paramSerializer; |
| 12995 | |
| 12996 | $browser.$$incOutstandingRequestCount('$http'); |
| 12997 | |
| 12998 | var requestInterceptors = []; |
| 12999 | var responseInterceptors = []; |
| 13000 | var promise = $q.resolve(config); |
| 13001 | |
| 13002 | // apply interceptors |
| 13003 | forEach(reversedInterceptors, function(interceptor) { |
| 13004 | if (interceptor.request || interceptor.requestError) { |
| 13005 | requestInterceptors.unshift(interceptor.request, interceptor.requestError); |
| 13006 | } |
| 13007 | if (interceptor.response || interceptor.responseError) { |
| 13008 | responseInterceptors.push(interceptor.response, interceptor.responseError); |
| 13009 | } |
| 13010 | }); |
| 13011 | |
| 13012 | promise = chainInterceptors(promise, requestInterceptors); |
| 13013 | promise = promise.then(serverRequest); |
| 13014 | promise = chainInterceptors(promise, responseInterceptors); |
| 13015 | promise = promise.finally(completeOutstandingRequest); |
| 13016 | |
| 13017 | return promise; |
| 13018 | |
| 13019 | |
| 13020 | function chainInterceptors(promise, interceptors) { |
| 13021 | for (var i = 0, ii = interceptors.length; i < ii;) { |
| 13022 | var thenFn = interceptors[i++]; |
| 13023 | var rejectFn = interceptors[i++]; |
| 13024 | |
| 13025 | promise = promise.then(thenFn, rejectFn); |
| 13026 | } |
| 13027 | |
| 13028 | interceptors.length = 0; |
| 13029 | |
| 13030 | return promise; |
no test coverage detected