diff --git a/packages/common/http/src/params.ts b/packages/common/http/src/params.ts index d43d91adbd..30caa33cde 100755 --- a/packages/common/http/src/params.ts +++ b/packages/common/http/src/params.ts @@ -224,9 +224,15 @@ export class HttpParams { return this.keys() .map(key => { const eKey = this.encoder.encodeKey(key); + // `a: ['1']` produces `'a=1'` + // `b: []` produces `''` + // `c: ['1', '2']` produces `'c=1&c=2'` return this.map !.get(key) !.map(value => eKey + '=' + this.encoder.encodeValue(value)) .join('&'); }) + // filter out empty values because `b: []` produces `''` + // which results in `a=1&&c=1&c=2` instead of `a=1&c=1&c=2` if we don't + .filter(param => param !== '') .join('&'); } diff --git a/packages/common/http/test/params_spec.ts b/packages/common/http/test/params_spec.ts index 669959c851..9c99771043 100644 --- a/packages/common/http/test/params_spec.ts +++ b/packages/common/http/test/params_spec.ts @@ -76,5 +76,20 @@ import {HttpParams} from '@angular/common/http/src/params'; expect(body.keys()).toEqual(['a', 'b', 'c', 'd']); }); }); + + describe('toString', () => { + it('should stringify string params', () => { + const body = new HttpParams({fromObject: {a: '', b: '2', c: '3'}}); + expect(body.toString()).toBe('a=&b=2&c=3'); + }); + it('should stringify array params', () => { + const body = new HttpParams({fromObject: {a: '', b: ['21', '22'], c: '3'}}); + expect(body.toString()).toBe('a=&b=21&b=22&c=3'); + }); + it('should stringify empty array params', () => { + const body = new HttpParams({fromObject: {a: '', b: [], c: '3'}}); + expect(body.toString()).toBe('a=&c=3'); + }); + }); }); }