angular: HttpClient sends the wrong Accept header
🐞 bug report
Affected Package
@angular/common/http
Is this a regression?
Not sure
Description
The get<T>
method on an HttpClient will send the following Accept header with a request if you do not override it:
Accept: application/json, text/plain, */*
This suggests that it is OK if the server responds with text/plain (i.e., not JSON).
However, if the server responds with something similar to this (truncated), you will get an error.
Content-Type: text/plain; charset=utf-8
59fb693e0302a519e0974379
Clearly text/plain is not a valid response type, and the default Accept header should be:
Accept: application/json
🔬 Minimal Reproduction
I will add this later if deemed necessary.
🔥 Exception or Error
"error": {
"name": "SyntaxError",
"message": "Unexpected token f in JSON at position 2"
}
🌍 Your Environment
Angular Version:
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.3.2
Node: 10.14.1
OS: win32 x64
Angular: 7.2.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.2
@angular-devkit/build-angular 0.13.2
@angular-devkit/build-optimizer 0.13.2
@angular-devkit/build-webpack 0.13.2
@angular-devkit/core 7.3.2
@angular-devkit/schematics 7.3.2
@angular/cdk 7.3.2
@angular/cli 7.3.2
@angular/material 7.3.2
@ngtools/webpack 7.3.2
@schematics/angular 7.3.2
@schematics/update 0.13.2
rxjs 6.4.0
typescript 3.2.4
webpack 4.29.5
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (6 by maintainers)
@m-henderson Well I’ve found this issue 5 months after the last comment and figured I’d make the repro myself: https://stackblitz.com/edit/angular-issue-repro2-fjrew5 here you go
Just open dev tools and you can see that the GET has
accept: application/json, text/plain, */*
Which is a lie. It will not, in fact, accept anything other than application/json. If HttpClient will attempt to parse the response as JSON by default, then it should not send anything other than
application/json
in theaccept
header by default. Right?The
text/plain, */*
was added toapplication/json
primarily to support legacy backends, especially those serving static.json
files and not recognizing them and serving them with a proper mime type.It might be safe these days to just default to
application/json
and provide an api to turn on legacy header support (or just document how to set the header for legacy servers).However, that would still not solve the problem with preloading captured in https://github.com/angular/angular/issues/34899#issuecomment-660492552. Defaulting to nothing at all might be too breaky though… so maybe the best path forward is to:
application/json
,Accept
header for non-standard backends, andAccept
header at allThoughts?
BTW, adding this header also (currently) breaks HTTP2 preloading. See https://github.com/angular/angular/issues/34899#issuecomment-660492552 for details.
@hungnguyenmanh82 It’s not a bug. It works as designed and was many times explained why this way has been chosen. Btw,
httpClient.get<HttpResponse>(url)
is just typing - it doesn’t affect the run-time code in any way.Yes I will do that