* @ngdoc service * @kind function * @name $http * @requires ng.$httpBackend * @requires $cacheFactory * @requires $rootScope * @requires $q * @requires $injector * * @description * The `$http` service is a core Angular service that facilitates commun
(requestConfig)
| 12186 | </example> |
| 12187 | */ |
| 12188 | function $http(requestConfig) { |
| 12189 | |
| 12190 | if (!isObject(requestConfig)) { |
| 12191 | throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig); |
| 12192 | } |
| 12193 | |
| 12194 | if (!isString($sce.valueOf(requestConfig.url))) { |
| 12195 | throw minErr('$http')('badreq', 'Http request configuration url must be a string or a $sce trusted object. Received: {0}', requestConfig.url); |
| 12196 | } |
| 12197 | |
| 12198 | var config = extend({ |
| 12199 | method: 'get', |
| 12200 | transformRequest: defaults.transformRequest, |
| 12201 | transformResponse: defaults.transformResponse, |
| 12202 | paramSerializer: defaults.paramSerializer, |
| 12203 | jsonpCallbackParam: defaults.jsonpCallbackParam |
| 12204 | }, requestConfig); |
| 12205 | |
| 12206 | config.headers = mergeHeaders(requestConfig); |
| 12207 | config.method = uppercase(config.method); |
| 12208 | config.paramSerializer = isString(config.paramSerializer) ? |
| 12209 | $injector.get(config.paramSerializer) : config.paramSerializer; |
| 12210 | |
| 12211 | $browser.$$incOutstandingRequestCount(); |
| 12212 | |
| 12213 | var requestInterceptors = []; |
| 12214 | var responseInterceptors = []; |
| 12215 | var promise = $q.resolve(config); |
| 12216 | |
| 12217 | // apply interceptors |
| 12218 | forEach(reversedInterceptors, function(interceptor) { |
| 12219 | if (interceptor.request || interceptor.requestError) { |
| 12220 | requestInterceptors.unshift(interceptor.request, interceptor.requestError); |
| 12221 | } |
| 12222 | if (interceptor.response || interceptor.responseError) { |
| 12223 | responseInterceptors.push(interceptor.response, interceptor.responseError); |
| 12224 | } |
| 12225 | }); |
| 12226 | |
| 12227 | promise = chainInterceptors(promise, requestInterceptors); |
| 12228 | promise = promise.then(serverRequest); |
| 12229 | promise = chainInterceptors(promise, responseInterceptors); |
| 12230 | promise = promise.finally(completeOutstandingRequest); |
| 12231 | |
| 12232 | return promise; |
| 12233 | |
| 12234 | |
| 12235 | function chainInterceptors(promise, interceptors) { |
| 12236 | for (var i = 0, ii = interceptors.length; i < ii;) { |
| 12237 | var thenFn = interceptors[i++]; |
| 12238 | var rejectFn = interceptors[i++]; |
| 12239 | |
| 12240 | promise = promise.then(thenFn, rejectFn); |
| 12241 | } |
| 12242 | |
| 12243 | interceptors.length = 0; |
| 12244 | |
| 12245 | return promise; |
no test coverage detected