@@ -220,8 +220,8 @@ function $HttpProvider() {
220
220
**/
221
221
var interceptorFactories = this . interceptors = [ ] ;
222
222
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 ) {
225
225
226
226
var defaultCache = $cacheFactory ( '$http' ) ;
227
227
@@ -1136,28 +1136,61 @@ function $HttpProvider() {
1136
1136
1137
1137
1138
1138
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 ) {
1140
1175
var parts = [ ] ;
1176
+
1177
+ if ( ! params ) return '' ;
1178
+
1141
1179
forEachSorted ( params , function ( value , key ) {
1142
1180
if ( value === null || isUndefined ( value ) ) return ;
1143
1181
if ( ! isArray ( value ) ) value = [ value ] ;
1144
1182
1145
1183
forEach ( value , function ( v ) {
1146
1184
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 ) ;
1152
1186
}
1153
- parts . push ( encodeUriQuery ( key ) + '=' +
1154
- encodeUriQuery ( v ) ) ;
1187
+ parts . push ( encodeUriQuery ( key ) + '=' + encodeUriQuery ( v ) ) ;
1155
1188
} ) ;
1156
1189
} ) ;
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
+ } ;
1163
1196
}
0 commit comments