Repository navigation
Closed
Labels
area: common/httpIssues related to HTTP and HTTP Client
Milestone
Description
Which @angular/* package(s) are the source of the bug?
common
Is this a regression?
Yes
Description
I was having an error locally that I was not able to reproduce it in production.
I am in a big project with a lot of dependencies, and was Migrating from Angular 13 to 14.
This is the error:
app.module.ts:71 ERROR Error: Unexpected value of the `Content-Length` header provided. Expecting either a string or an array, but got: `281`.
at assertValidHeaders (http.mjs:287:19)
at HttpHeaders.lazyInit (http.mjs:100:21)
at HttpHeaders.init (http.mjs:207:22)
at HttpHeaders.copyFrom (http.mjs:217:15)
at HttpHeaders.init (http.mjs:204:22)
at HttpHeaders.has (http.mjs:125:14)
at HttpXSRFInterceptor.intercept (http-xsrf-interceptor.ts:36:40)
After some hours debugging, I started reading the source code to see what's happening and I found this lines in headers.ts
/**
* Verifies that the headers object has the right shape: the values
* must be either strings or arrays. Throws an error if an invalid
* header value is present.
*/
function assertValidHeaders(headers) {
for (const [key, value] of Object.entries(headers)) {
if (typeof value !== 'string' && !Array.isArray(value)) {
throw new Error(`Unexpected value of the \`${key}\` header provided. ` +
`Expecting either a string or an array, but got: \`${value}\`.`);
}
}
}As far as i understood, this is only being called if we are in devMode. Check this lines:
if (typeof ngDevMode === 'undefined' || ngDevMode) {
assertValidHeaders(headers);
}I've got 3 questions:
- Is it correct that we're doing those kinds of evaluations in dev Environment and not in production envs? asking this because I'm running on dockers and my local is like my production environment and this is breaking my entire application, since its trowing an error, and some libraries methods are failing cause of this.
- Wouldn't be better if instead of trowing an error we just inform to the user using a console.error, or something like this? Those kind of lines are being evaluated when I do something like
request.headers.has('OtherHeaderName'). - Is there a way to fix this? this is the only thing preventing me to migrate to Angular 14. Didn't find a solution yet. What surpriced me the most is that I'm not seeing any request on the network tab, and I also have this line in the interceptor that's being executed:
// Setting a `host` header is being refused because it's unsafe. So let's just drop it.
let httpHeaders = new HttpHeaders(headers);
httpHeaders = httpHeaders.delete('Host');
httpHeaders = httpHeaders.delete('Content-Length');
request = request.clone({
headers: httpHeaders,
});
return next.handle(request);Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
app.module.ts:71 ERROR Error: Unexpected value of the `Content-Length` header provided. Expecting either a string or an array, but got: `281`.
at assertValidHeaders (http.mjs:287:19)
at HttpHeaders.lazyInit (http.mjs:100:21)
at HttpHeaders.init (http.mjs:207:22)
at HttpHeaders.copyFrom (http.mjs:217:15)
at HttpHeaders.init (http.mjs:204:22)
at HttpHeaders.has (http.mjs:125:14)
at HttpXSRFInterceptor.intercept (http-xsrf-interceptor.ts:36:40)
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 14.2.10
Node: 16.13.2
Package Manager: npm 8.1.2
OS: linux x64
Angular: 14.2.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1402.10
@angular-devkit/build-angular 14.2.10
@angular-devkit/core 14.2.10
@angular-devkit/schematics 14.2.10
@angular/cdk 14.2.7
@angular/cli 14.2.10
@schematics/angular 14.2.10
rxjs 6.6.7
typescript 4.6.4
Anything else?
No response
Metadata
Metadata
Assignees
Labels
area: common/httpIssues related to HTTP and HTTP Client
Activity
[-]Headers assertValidHeaders is breacking my local environment.[/-][+]Headers assertValidHeaders is breaking my local environment.[/+][-]Headers assertValidHeaders is breaking my local environment.[/-][+]Headers assertValidHeaders breaks local environments.[/+][-]Headers assertValidHeaders breaks local environments.[/-][+]Headers assertValidHeaders method breaks local environments.[/+]JeanMeche commentedon Mar 7, 2023
Hi,
To answer the first 2 points : to reduce and optimize the payload size, multiple errors and validations/assertions from the Angular framework are tree shaken at build time by the optimizer.
To get to the bottom of you problem. I looks like #46059 has introduced this regression.
assertValidHeadersshould be accepting numbers.But since the Angular and the TS typings only allow strings in header values, how did you even get there ? Could you maybe create a repro on stackblitz ?
danilobassi8 commentedon Mar 7, 2023
I will try, but I dont even know why this is happening. I am using the debugger, and when I console log my headers they look like this:
fix: prevent headers to throw an error on initialization for numerica…
danilobassi8 commentedon Mar 9, 2023
Hi, I don't know how the usual flow is. But I opened a PR to the main branch with some changes to fix that issue.
Thanks in advance.
AurelienLoyer commentedon Mar 9, 2023
Same issue on our side with a simple interceptor getting the header... 😭
And the famous interceptor 👇
jbmgrtn commentedon Mar 9, 2023
According to the code I see in my browser
shouldn't this method be called on
headers.headersinstead becauseheadersis{ "normalizedNames": [], "lazyUpdate": [ { "name": "authorization", "op": "d" } ], "headers": [], "lazyInit": { "normalizedNames": [ [ "authorization", "Authorization" ] ], "lazyUpdate": null, "headers": [ [ "authorization", [ "REDACTED" ] ] ], "lazyInit": null } }danilobassi8 commentedon Mar 9, 2023
oh.. @AurelienLoyer my PR wont cover that case because you are sending a Map.
fix(http): prevent headers from throwing an error when initializing n…
fix(http): prevent headers from throwing an error when initializing n…
angular-automatic-lock-bot commentedon May 14, 2023
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.