Skip to content

Commit f6f00f0

Browse files
feat($httpUrlParams): introduce new service abstracting params serialization
Closes angular#7429 Closes angular#9224
1 parent 0c541cf commit f6f00f0

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

src/AngularPublic.js

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
$IntervalProvider,
6868
$HttpProvider,
6969
$HttpBackendProvider,
70+
$HttpUrlParamsProvider,
7071
$LocationProvider,
7172
$LogProvider,
7273
$ParseProvider,
@@ -224,6 +225,7 @@ function publishExternalAPI(angular) {
224225
$interval: $IntervalProvider,
225226
$http: $HttpProvider,
226227
$httpBackend: $HttpBackendProvider,
228+
$httpUrlParams: $HttpUrlParamsProvider,
227229
$location: $LocationProvider,
228230
$log: $LogProvider,
229231
$parse: $ParseProvider,

src/ng/http.js

+49-16
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ function $HttpProvider() {
220220
**/
221221
var interceptorFactories = this.interceptors = [];
222222

223-
this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
224-
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {
223+
this.$get = ['$httpBackend', '$httpUrlParams', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
224+
function($httpBackend, $httpUrlParams, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {
225225

226226
var defaultCache = $cacheFactory('$http');
227227

@@ -1136,28 +1136,61 @@ function $HttpProvider() {
11361136

11371137

11381138
function buildUrl(url, params) {
1139-
if (!params) return url;
1139+
params = $httpUrlParams.serialize(params);
1140+
if (params) {
1141+
url += ((url.indexOf('?') == -1) ? '?' : '&') + params;
1142+
}
1143+
return url;
1144+
}
1145+
}];
1146+
}
1147+
1148+
/**
1149+
* @ngdoc provider
1150+
* @name $httpUrlParamsProvider
1151+
* @description
1152+
* Use `$httpUrlParamsProvider` to change the default behavior of the {@link ng.$httpUrlParams $httpUrlParams} service.
1153+
* */
1154+
function $HttpUrlParamsProvider() {
1155+
1156+
/**
1157+
* @ngdoc service
1158+
* @name $httpUrlParams
1159+
*
1160+
* @description
1161+
* The `$httpUrlParams` service is responsible for serializing request params
1162+
* (expressed as a JavaScript object) to a string.
1163+
* It abstracts the way in which request params are transformed, grouped, encoded etc.
1164+
*
1165+
* Normally you wouldn't use this service directly in the application's code but rather override this service
1166+
* to enable custom serialization schemas for URL parameters.
1167+
*
1168+
* The `$httpUrlParams` service comes handy while unit testing with {@link ngMock.$httpBackend $httpBackend mock},
1169+
* as one can inject `$httpUrlParams` into a test and serialize URL params as {@link ng.$http $http} service.
1170+
*/
1171+
this.$get = function() {
1172+
var HttpUrlParams = {};
1173+
1174+
HttpUrlParams.serialize = function(params) {
11401175
var parts = [];
1176+
1177+
if (!params) return '';
1178+
11411179
forEachSorted(params, function(value, key) {
11421180
if (value === null || isUndefined(value)) return;
11431181
if (!isArray(value)) value = [value];
11441182

11451183
forEach(value, function(v) {
11461184
if (isObject(v)) {
1147-
if (isDate(v)) {
1148-
v = v.toISOString();
1149-
} else {
1150-
v = toJson(v);
1151-
}
1185+
v = isDate(v) ? v.toISOString() : toJson(v);
11521186
}
1153-
parts.push(encodeUriQuery(key) + '=' +
1154-
encodeUriQuery(v));
1187+
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));
11551188
});
11561189
});
1157-
if (parts.length > 0) {
1158-
url += ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
1159-
}
1160-
return url;
1161-
}
1162-
}];
1190+
1191+
return parts.join('&');
1192+
};
1193+
1194+
return HttpUrlParams;
1195+
};
11631196
}

0 commit comments

Comments
 (0)