@@ -214,8 +214,8 @@ function $HttpProvider() {
214
214
**/
215
215
var interceptorFactories = this . interceptors = [ ] ;
216
216
217
- this . $get = [ '$httpBackend' , '$browser' , '$cacheFactory' , '$rootScope' , '$q' , '$injector' ,
218
- function ( $httpBackend , $browser , $cacheFactory , $rootScope , $q , $injector ) {
217
+ this . $get = [ '$httpBackend' , '$httpUrlParams' , '$ browser', '$cacheFactory' , '$rootScope' , '$q' , '$injector' ,
218
+ function ( $httpBackend , $httpUrlParams , $ browser, $cacheFactory , $rootScope , $q , $injector ) {
219
219
220
220
var defaultCache = $cacheFactory ( '$http' ) ;
221
221
@@ -1130,28 +1130,61 @@ function $HttpProvider() {
1130
1130
1131
1131
1132
1132
function buildUrl ( url , params ) {
1133
- if ( ! params ) return url ;
1133
+ params = $httpUrlParams . serialize ( params ) ;
1134
+ if ( params ) {
1135
+ url += ( ( url . indexOf ( '?' ) == - 1 ) ? '?' : '&' ) + params ;
1136
+ }
1137
+ return url ;
1138
+ }
1139
+ } ] ;
1140
+ }
1141
+
1142
+ /**
1143
+ * @ngdoc provider
1144
+ * @name $httpUrlParamsProvider
1145
+ * @description
1146
+ * Use `$httpUrlParamsProvider` to change the default behavior of the {@link ng.$httpUrlParams $httpUrlParams} service.
1147
+ * */
1148
+ function $HttpUrlParamsProvider ( ) {
1149
+
1150
+ /**
1151
+ * @ngdoc service
1152
+ * @name $httpUrlParams
1153
+ *
1154
+ * @description
1155
+ * The `$httpUrlParams` service is responsible for serializing request params
1156
+ * (expressed as a JavaScript object) to a string.
1157
+ * It abstracts the way in which request params are transformed, grouped, encoded etc.
1158
+ *
1159
+ * Normally you wouldn't use this service directly in the application's code but rather override this service
1160
+ * to enable custom serialization schemas for URL parameters.
1161
+ *
1162
+ * The `$httpUrlParams` service comes handy while unit testing with {@link ngMock.$httpBackend $httpBackend mock},
1163
+ * as one can inject `$httpUrlParams` into a test and serialize URL params as {@link ng.$http $http} service.
1164
+ */
1165
+ this . $get = function ( ) {
1166
+ var HttpUrlParams = { } ;
1167
+
1168
+ HttpUrlParams . serialize = function ( params ) {
1134
1169
var parts = [ ] ;
1170
+
1171
+ if ( ! params ) return '' ;
1172
+
1135
1173
forEachSorted ( params , function ( value , key ) {
1136
1174
if ( value === null || isUndefined ( value ) ) return ;
1137
1175
if ( ! isArray ( value ) ) value = [ value ] ;
1138
1176
1139
1177
forEach ( value , function ( v ) {
1140
1178
if ( isObject ( v ) ) {
1141
- if ( isDate ( v ) ) {
1142
- v = v . toISOString ( ) ;
1143
- } else {
1144
- v = toJson ( v ) ;
1145
- }
1179
+ v = isDate ( v ) ? v . toISOString ( ) : toJson ( v ) ;
1146
1180
}
1147
- parts . push ( encodeUriQuery ( key ) + '=' +
1148
- encodeUriQuery ( v ) ) ;
1181
+ parts . push ( encodeUriQuery ( key ) + '=' + encodeUriQuery ( v ) ) ;
1149
1182
} ) ;
1150
1183
} ) ;
1151
- if ( parts . length > 0 ) {
1152
- url += ( ( url . indexOf ( '?' ) == - 1 ) ? '?' : '&' ) + parts . join ( '&' ) ;
1153
- }
1154
- return url ;
1155
- }
1156
- } ] ;
1184
+
1185
+ return parts . join ( '&' ) ;
1186
+ } ;
1187
+
1188
+ return HttpUrlParams ;
1189
+ } ;
1157
1190
}
0 commit comments