Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 79af2ba

Browse files
tdavisIgorMinar
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 #1363
1 parent 610927d commit 79af2ba

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
@@ -758,10 +758,15 @@ function $HttpProvider() {
758758
var parts = [];
759759
forEachSorted(params, function(value, key) {
760760
if (value == null || value == undefined) return;
761-
if (isObject(value)) {
762-
value = toJson(value);
763-
}
764-
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
761+
if (!isArray(value)) value = [value];
762+
763+
forEach(value, function(v) {
764+
if (isObject(v)) {
765+
v = toJson(v);
766+
}
767+
parts.push(encodeURIComponent(key) + '=' +
768+
encodeURIComponent(v));
769+
});
765770
});
766771
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
767772
}

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)