* @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)
| 12906 | </example> |
| 12907 | */ |
| 12908 | function $http(requestConfig) { |
| 12909 | |
| 12910 | if (!isObject(requestConfig)) { |
| 12911 | throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig); |
| 12912 | } |
| 12913 | |
| 12914 | if (!isString($sce.valueOf(requestConfig.url))) { |
| 12915 | throw minErr('$http')('badreq', 'Http request configuration url must be a string or a $sce trusted object. Received: {0}', requestConfig.url); |
| 12916 | } |
| 12917 | |
| 12918 | var config = extend({ |
| 12919 | method: 'get', |
| 12920 | transformRequest: defaults.transformRequest, |
| 12921 | transformResponse: defaults.transformResponse, |
| 12922 | paramSerializer: defaults.paramSerializer, |
| 12923 | jsonpCallbackParam: defaults.jsonpCallbackParam |
| 12924 | }, requestConfig); |
| 12925 | |
| 12926 | config.headers = mergeHeaders(requestConfig); |
| 12927 | config.method = uppercase(config.method); |
| 12928 | config.paramSerializer = isString(config.paramSerializer) ? |
| 12929 | $injector.get(config.paramSerializer) : config.paramSerializer; |
| 12930 | |
| 12931 | $browser.$$incOutstandingRequestCount('$http'); |
| 12932 | |
| 12933 | var requestInterceptors = []; |
| 12934 | var responseInterceptors = []; |
| 12935 | var promise = $q.resolve(config); |
| 12936 | |
| 12937 | // apply interceptors |
| 12938 | forEach(reversedInterceptors, function(interceptor) { |
| 12939 | if (interceptor.request || interceptor.requestError) { |
| 12940 | requestInterceptors.unshift(interceptor.request, interceptor.requestError); |
| 12941 | } |
| 12942 | if (interceptor.response || interceptor.responseError) { |
| 12943 | responseInterceptors.push(interceptor.response, interceptor.responseError); |
| 12944 | } |
| 12945 | }); |
| 12946 | |
| 12947 | promise = chainInterceptors(promise, requestInterceptors); |
| 12948 | promise = promise.then(serverRequest); |
| 12949 | promise = chainInterceptors(promise, responseInterceptors); |
| 12950 | promise = promise.finally(completeOutstandingRequest); |
| 12951 | |
| 12952 | return promise; |
| 12953 | |
| 12954 | |
| 12955 | function chainInterceptors(promise, interceptors) { |
| 12956 | for (var i = 0, ii = interceptors.length; i < ii;) { |
| 12957 | var thenFn = interceptors[i++]; |
| 12958 | var rejectFn = interceptors[i++]; |
| 12959 | |
| 12960 | promise = promise.then(thenFn, rejectFn); |
| 12961 | } |
| 12962 | |
| 12963 | interceptors.length = 0; |
| 12964 | |
| 12965 | return promise; |
no test coverage detected