From 2df9e785415dde7ee0f9dadb3b60d5e816420d9c Mon Sep 17 00:00:00 2001 From: Ajit Singh Date: Tue, 30 Jun 2020 21:35:30 +0530 Subject: [PATCH 1/3] feat(common): add URLSearchParams to request body URLSearch params are by default supported in the browser but are not supported by angular/http package added support for URLSearchParams Fixes #36317 --- packages/common/http/src/request.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/common/http/src/request.ts b/packages/common/http/src/request.ts index 5d57aa4d7b02a..0bce610115ec4 100644 --- a/packages/common/http/src/request.ts +++ b/packages/common/http/src/request.ts @@ -68,6 +68,15 @@ function isFormData(value: any): value is FormData { return typeof FormData !== 'undefined' && value instanceof FormData; } +/** + * Safely assert whether the given value is a URLSearchParams instance. + * + * In some execution environments URLSearchParams is not defined. + */ +function isUrlSearchParams(value: any): value is URLSearchParams { + return typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams; +} + /** * An outgoing HTTP request with an optional typed body. * @@ -273,7 +282,7 @@ export class HttpRequest { // Check whether the body is already in a serialized form. If so, // it can just be returned directly. if (isArrayBuffer(this.body) || isBlob(this.body) || isFormData(this.body) || - typeof this.body === 'string') { + isUrlSearchParams(this.body) || typeof this.body === 'string') { return this.body; } // Check whether the body is an instance of HttpUrlEncodedParams. From 9307516431994ff64d7a8404abe28e38eceff782 Mon Sep 17 00:00:00 2001 From: Ajit Singh Date: Tue, 30 Jun 2020 21:43:21 +0530 Subject: [PATCH 2/3] fixup! feat(common): add URLSearchParams to request body --- packages/common/http/test/request_spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/common/http/test/request_spec.ts b/packages/common/http/test/request_spec.ts index 9a0d91f3cac29..9f69c53354fcf 100644 --- a/packages/common/http/test/request_spec.ts +++ b/packages/common/http/test/request_spec.ts @@ -143,6 +143,10 @@ const TEST_STRING = `I'm a body!`; const body = new ArrayBuffer(4); expect(baseReq.clone({body}).serializeBody()).toBe(body); }); + it('passes URLSearchParams through', () => { + const body = new URLSearchParams("foo=1&bar=2"); + expect(baseReq.clone({body}).serializeBody()).toBe(body); + }); it('passes strings through', () => { const body = 'hello world'; expect(baseReq.clone({body}).serializeBody()).toBe(body); From d0088be19dd40928cfd275aff186e80bfe845c22 Mon Sep 17 00:00:00 2001 From: Ajit Singh Date: Tue, 30 Jun 2020 22:07:01 +0530 Subject: [PATCH 3/3] fixup! feat(common): add URLSearchParams to request body --- packages/common/http/test/request_spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/http/test/request_spec.ts b/packages/common/http/test/request_spec.ts index 9f69c53354fcf..f1800092e1fd0 100644 --- a/packages/common/http/test/request_spec.ts +++ b/packages/common/http/test/request_spec.ts @@ -144,7 +144,7 @@ const TEST_STRING = `I'm a body!`; expect(baseReq.clone({body}).serializeBody()).toBe(body); }); it('passes URLSearchParams through', () => { - const body = new URLSearchParams("foo=1&bar=2"); + const body = new URLSearchParams('foo=1&bar=2'); expect(baseReq.clone({body}).serializeBody()).toBe(body); }); it('passes strings through', () => {