Skip to content

Commit

Permalink
fix(common): add right ContentType for boolean values with HttpClient…
Browse files Browse the repository at this point in the history
… request body(angular#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.
  • Loading branch information
gopal-jayaraman committed Apr 29, 2021
1 parent ca0fcd6 commit 97f144d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/common/http/src/request.ts
Expand Up @@ -323,7 +323,7 @@ export class HttpRequest<T> {
return 'application/x-www-form-urlencoded;charset=UTF-8';
}
// Arrays, objects, and numbers will be encoded as JSON.
if (typeof this.body === 'object' || typeof this.body === 'number' ||
if (typeof this.body === 'object' || typeof this.body === 'number' || typeof this.body === 'boolean' ||
Array.isArray(this.body)) {
return 'application/json';
}
Expand Down
4 changes: 4 additions & 0 deletions packages/common/http/test/request_spec.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 97f144d

Please sign in to comment.