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. diff --git a/packages/common/http/test/request_spec.ts b/packages/common/http/test/request_spec.ts index 9a0d91f3cac29..f1800092e1fd0 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);