diff --git a/packages/common/http/src/params.ts b/packages/common/http/src/params.ts index 87cdedd61c..b4841570c8 100644 --- a/packages/common/http/src/params.ts +++ b/packages/common/http/src/params.ts @@ -73,7 +73,10 @@ export class HttpUrlEncodingCodec implements HttpParameterCodec { function paramParser(rawParams: string, codec: HttpParameterCodec): Map { const map = new Map(); if (rawParams.length > 0) { - const params: string[] = rawParams.split('&'); + // The `window.location.search` can be used while creating an instance of the `HttpParams` class + // (e.g. `new HttpParams({ fromString: window.location.search })`). The `window.location.search` + // may start with the `?` char, so we strip it if it's present. + const params: string[] = rawParams.replace(/^\?/, '').split('&'); params.forEach((param: string) => { const eqIdx = param.indexOf('='); const [key, val]: string[] = eqIdx == -1 ? diff --git a/packages/common/http/test/params_spec.ts b/packages/common/http/test/params_spec.ts index 9cd8422624..60d20421a0 100644 --- a/packages/common/http/test/params_spec.ts +++ b/packages/common/http/test/params_spec.ts @@ -21,6 +21,19 @@ import {HttpParams} from '@angular/common/http/src/params'; expect(body.getAll('a')).toEqual(['b']); expect(body.getAll('c')).toEqual(['d', 'e']); }); + + it('should ignore question mark in a url', () => { + const body = new HttpParams({fromString: '?a=b&c=d&c=e'}); + expect(body.getAll('a')).toEqual(['b']); + expect(body.getAll('c')).toEqual(['d', 'e']); + }); + + it('should only remove question mark at the beginning of the params', () => { + const body = new HttpParams({fromString: '?a=b&c=d&?e=f'}); + expect(body.getAll('a')).toEqual(['b']); + expect(body.getAll('c')).toEqual(['d']); + expect(body.getAll('?e')).toEqual(['f']); + }); }); describe('lazy mutation', () => {