New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpClient - incorrect ContentType for boolean #38924
Comments
Yep, it looks like you're right. Thanks for the detailed explanation and examples; it makes reports like this really simple to evaluate. I'm setting the priority pretty low though because the recommendation is to not use texts that are neither objects nor arrays:
|
@gopal-jayaraman - This report is for |
Thanks for the info @atscott , I will be raising a PR for this. |
… 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.
… 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.
… 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.
… 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.
… 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.
This was fixed in #41885 in Angular 11.2.13! |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 bug report
Affected Package
The issue is caused by package @angular/common/httpIs this a regression?
No.Description
TLDR:
A boolean as body is seen as
text/plain
where is should be seen asapplication/json
, since it is valid JSON, like numbers. See:https://jsonlint.com/
https://www.json.org/json-en.html
Detailed description:
The
post()
method ofHttpClient
(and other request methods) accept a body method argument of typeany
. When the HTTP request is about to execute,HttpClient
will determine the content type ofbody
, so that it can set theContent-Type
header to for exampleapplication/json
ortext/plain
.When an object is used as
body
, HttpClient will send it asapplication/json
as expected.When a plain number is used as
body
(e.g.123
, without quotes), HttpClient will also send it asapplication/json
, because even a plain number (without curly braces and without quotes) is valid JSON.When a TypeScript string is used as
body
argument (e.g."abc"
), HttpClient will send it astext/plain
because it will be send without quotes in the HTTP request body, and thus isn't valid JSON. So it will usetext/plain
as expected.But when boolean is used, this behavior is different. When a plain boolean (e.g.
true
) is used asbody
argument, HttpClient will send it astext/plain
even though a simple plain boolean is valid JSON.The code where this problems is caused is:
angular/packages/common/http/src/request.ts
Line 295 in 2686219
Plain objects, numbers, booleans and arrays values are all valid JSON code. Whereas a plain string without quotes it not valid JSON.
So plain objects, numbers, booleans and arrays values should be send as
application/json
and strings astext/plain
. But in HttpClient this behavior unexpectedly differs for booleans.🔬 Minimal Reproduction
https://stackblitz.com/edit/angular-ivy-umexba?file=src/app/app.component.ts
🌍 Your Environment
Angular Version:
The text was updated successfully, but these errors were encountered: