From 1cb2bdbe3cc0fbf34dd52f6ab84ac1880f63d869 Mon Sep 17 00:00:00 2001 From: Gopal Jayaraman Date: Thu, 29 Apr 2021 22:57:16 +0530 Subject: [PATCH] fix(common): add right ContentType for boolean values with HttpClient request body(#38924) currently a boolean as body is seen as text/plain, where is should be seen as application/json, since it is valid JSON, like numbers. --- packages/common/http/src/request.ts | 4 ++-- packages/common/http/test/request_spec.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/common/http/src/request.ts b/packages/common/http/src/request.ts index bb49113b0ad71..5d57aa4d7b02a 100644 --- a/packages/common/http/src/request.ts +++ b/packages/common/http/src/request.ts @@ -322,9 +322,9 @@ export class HttpRequest { if (this.body instanceof HttpParams) { return 'application/x-www-form-urlencoded;charset=UTF-8'; } - // Arrays, objects, and numbers will be encoded as JSON. + // Arrays, objects, boolean and numbers will be encoded as JSON. if (typeof this.body === 'object' || typeof this.body === 'number' || - Array.isArray(this.body)) { + typeof this.body === 'boolean') { return 'application/json'; } // No type could be inferred. diff --git a/packages/common/http/test/request_spec.ts b/packages/common/http/test/request_spec.ts index 7519b47e00479..9a0d91f3cac29 100644 --- a/packages/common/http/test/request_spec.ts +++ b/packages/common/http/test/request_spec.ts @@ -129,6 +129,10 @@ const TEST_STRING = `I'm a body!`; const req = baseReq.clone({body: {data: 'test data'}}); expect(req.detectContentTypeHeader()).toBe('application/json'); }); + it('handles boolean as json', () => { + const req = baseReq.clone({body: true}); + expect(req.detectContentTypeHeader()).toBe('application/json'); + }); }); describe('body serialization', () => { const baseReq = new HttpRequest('POST', '/test', null);