Skip to content

Commit 8d2ac16

Browse files
tdavismarknadig
authored andcommitted
fix($http): config.param should expand array values properly
Today, calling e.g. $http(url, { params: { a: [1,2,3] } }) results in a query string like "?a=%5B1%2C2%2C3%5D" which is undesirable. This commit enhances buildURL to createa query string like "?a=1&a=2&a=3". BREAKING CHANGE: if the server relied on the buggy behavior then either the backend should be fixed or a simple serialization of the array should be done on the client before calling the $http service. Closes angular#1363
1 parent 79b0084 commit 8d2ac16

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/ng/http.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,15 @@ function $HttpProvider() {
752752
var parts = [];
753753
forEachSorted(params, function(value, key) {
754754
if (value == null || value == undefined) return;
755-
if (isObject(value)) {
756-
value = toJson(value);
757-
}
758-
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
755+
if (!isArray(value)) value = [value];
756+
757+
forEach(value, function(v) {
758+
if (isObject(v)) {
759+
v = toJson(v);
760+
}
761+
parts.push(encodeURIComponent(key) + '=' +
762+
encodeURIComponent(v));
763+
});
759764
});
760765
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
761766
}

test/ng/httpSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ describe('$http', function() {
147147
$httpBackend.expect('GET', '/url?a=1&b=%7B%22c%22%3A3%7D').respond('');
148148
$http({url: '/url', params: {a:1, b:{c:3}}, method: 'GET'});
149149
}));
150+
151+
152+
it('should expand arrays in params map', inject(function($httpBackend, $http) {
153+
$httpBackend.expect('GET', '/url?a=1&a=2&a=3').respond('');
154+
$http({url: '/url', params: {a: [1,2,3]}, method: 'GET'});
155+
}));
150156
});
151157

152158

0 commit comments

Comments
 (0)