* @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)
| 10509 | </example> |
| 10510 | */ |
| 10511 | function $http(requestConfig) { |
| 10512 | |
| 10513 | if (!angular.isObject(requestConfig)) { |
| 10514 | throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig); |
| 10515 | } |
| 10516 | |
| 10517 | if (!isString(requestConfig.url)) { |
| 10518 | throw minErr('$http')('badreq', 'Http request configuration url must be a string. Received: {0}', requestConfig.url); |
| 10519 | } |
| 10520 | |
| 10521 | var config = extend({ |
| 10522 | method: 'get', |
| 10523 | transformRequest: defaults.transformRequest, |
| 10524 | transformResponse: defaults.transformResponse, |
| 10525 | paramSerializer: defaults.paramSerializer |
| 10526 | }, requestConfig); |
| 10527 | |
| 10528 | config.headers = mergeHeaders(requestConfig); |
| 10529 | config.method = uppercase(config.method); |
| 10530 | config.paramSerializer = isString(config.paramSerializer) ? |
| 10531 | $injector.get(config.paramSerializer) : config.paramSerializer; |
| 10532 | |
| 10533 | var serverRequest = function(config) { |
| 10534 | var headers = config.headers; |
| 10535 | var reqData = transformData(config.data, headersGetter(headers), undefined, config.transformRequest); |
| 10536 | |
| 10537 | // strip content-type if data is undefined |
| 10538 | if (isUndefined(reqData)) { |
| 10539 | forEach(headers, function(value, header) { |
| 10540 | if (lowercase(header) === 'content-type') { |
| 10541 | delete headers[header]; |
| 10542 | } |
| 10543 | }); |
| 10544 | } |
| 10545 | |
| 10546 | if (isUndefined(config.withCredentials) && !isUndefined(defaults.withCredentials)) { |
| 10547 | config.withCredentials = defaults.withCredentials; |
| 10548 | } |
| 10549 | |
| 10550 | // send request |
| 10551 | return sendReq(config, reqData).then(transformResponse, transformResponse); |
| 10552 | }; |
| 10553 | |
| 10554 | var chain = [serverRequest, undefined]; |
| 10555 | var promise = $q.when(config); |
| 10556 | |
| 10557 | // apply interceptors |
| 10558 | forEach(reversedInterceptors, function(interceptor) { |
| 10559 | if (interceptor.request || interceptor.requestError) { |
| 10560 | chain.unshift(interceptor.request, interceptor.requestError); |
| 10561 | } |
| 10562 | if (interceptor.response || interceptor.responseError) { |
| 10563 | chain.push(interceptor.response, interceptor.responseError); |
| 10564 | } |
| 10565 | }); |
| 10566 | |
| 10567 | while (chain.length) { |
| 10568 | var thenFn = chain.shift(); |
no test coverage detected