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

Encoding of arrays in GET params. #3919

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,16 +966,17 @@ function $HttpProvider() {

function buildUrl(url, params) {
if (!params) return url;
var parts = [];
var parts = [], valueIsArray;
forEachSorted(params, function(value, key) {
if (value == null || value == undefined) return;
if (!isArray(value)) value = [value];
valueIsArray = isArray(value);
if (!valueIsArray) value = [value];

forEach(value, function(v) {
if (isObject(v)) {
v = toJson(v);
}
parts.push(encodeUriQuery(key) + '=' +
parts.push(encodeUriQuery(key) + (valueIsArray ? '[]=' : '=') +
encodeUriQuery(v));
});
});
Expand Down
8 changes: 7 additions & 1 deletion test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,17 @@ describe('$http', function() {


it('should expand arrays in params map', inject(function($httpBackend, $http) {
$httpBackend.expect('GET', '/url?a=1&a=2&a=3').respond('');
$httpBackend.expect('GET', '/url?a[]=1&a[]=2&a[]=3').respond('');
$http({url: '/url', params: {a: [1,2,3]}, method: 'GET'});
}));


it('should expand short arrays in params map', inject(function($httpBackend, $http) {
$httpBackend.expect('GET', '/url?a[]=1').respond('');
$http({url: '/url', params: {a: [1]}, method: 'GET'});
}));


it('should not encode @ in url params', function() {
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
//with regards to the character set (pchar) allowed in path segments
Expand Down
2 changes: 1 addition & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe("resource", function() {

it('should encode array params', function() {
var R = $resource('/Path/:a');
$httpBackend.expect('GET', '/Path/doh&foo?bar=baz1&bar=baz2').respond('{}');
$httpBackend.expect('GET', '/Path/doh&foo?bar[]=baz1&bar[]=baz2').respond('{}');
R.get({a: 'doh&foo', bar: ['baz1', 'baz2']});
});

Expand Down