Skip to content

Commit 922a602

Browse files
gopal-jayaramanmhevery
authored andcommitted
fix(common): add right ContentType for boolean values with HttpClient request body(#38924) (#41885)
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. PR Close #41885
1 parent d59330b commit 922a602

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

packages/common/http/src/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ export class HttpRequest<T> {
322322
if (this.body instanceof HttpParams) {
323323
return 'application/x-www-form-urlencoded;charset=UTF-8';
324324
}
325-
// Arrays, objects, and numbers will be encoded as JSON.
325+
// Arrays, objects, boolean and numbers will be encoded as JSON.
326326
if (typeof this.body === 'object' || typeof this.body === 'number' ||
327-
Array.isArray(this.body)) {
327+
typeof this.body === 'boolean') {
328328
return 'application/json';
329329
}
330330
// No type could be inferred.

packages/common/http/test/request_spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ const TEST_STRING = `I'm a body!`;
129129
const req = baseReq.clone({body: {data: 'test data'}});
130130
expect(req.detectContentTypeHeader()).toBe('application/json');
131131
});
132+
it('handles boolean as json', () => {
133+
const req = baseReq.clone({body: true});
134+
expect(req.detectContentTypeHeader()).toBe('application/json');
135+
});
132136
});
133137
describe('body serialization', () => {
134138
const baseReq = new HttpRequest('POST', '/test', null);

0 commit comments

Comments
 (0)