-1

In the docs for HttpClient, there's a mention of HttpObserve. However, when I Google for it, it seems (based on different blogs and forums) that it's been removed. It's definitely not present in Angular 9 (under @angular/common/http, at least). Various sources provide different suggestions and I sense that there's more assuming than knowing in those.

According to e.g. this declaration there's something of type HttpObserve supposed to be provided (or skipped, of course, which most people probably do). I've prepared the following sample.

const headers: HttpHeaders = new HttpHeaders();
const observe: HttpObserve = null;
const params: HttpParams = new HttpParams();
const reportProgress = false;
const responseType = "json";
const withCredentials = true;

VS Code marks the second line as incorrect as the type isn't recognized. I couldn't find a reliable, credible source of info on how to handle it, so I get cautious. There's also seemingly different responses depending on the version since 5th through 8th and not that many on the current 9th. Having time to research it in depth, I'm asking here.

What, if anything, should/could be provided as type for the observe parameter?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • It seems from [this (rude) comment](https://stackoverflow.com/questions/58679204/has-httpobserve-disappeared-in-angular-8-release-for-a-httpclient-get-request/58679837#comment110795019_58679204) that you did do research here - please *show your research* when you ask a question, otherwise other people end up revisiting what you already know. – jonrsharpe Jun 30 '20 at 10:32
  • I'll revise the comment immediately, since you mention that it's rude (which definitely wasn't my intention). Rudeness is not a trait I want to be part of, so my apologies for you needing to discover that and thanks for pointing it out to me (in a clear but not rude way, so hats off). As for the research, I always do it first but sometimes fail to explain it well enough. I realize that mentioning a number of unspecified blogs/fora presenting various ideas didn't do. I concluded that there was little result to mention. Please allow me to retry. – Konrad Viltersten Jun 30 '20 at 10:42

1 Answers1

1

You can see its definition in the source code (you can find it via GitHub search):

export type HttpObserve = 'body'|'events'|'response';

or listed out in the various overloads in the HttpClient API docs you linked to. It is present in Angular 9 (see e.g. 9.1.x tag), it's just not exposed as part of the module definition.

Again, this is a string literal type; the value must be one of those three string literals. For example, in the HTTP guide they show it being set to 'response' to read the whole response (rather than the default 'body'):

getConfigResponse(): Observable<HttpResponse<Config>> {
  return this.http.get<Config>(
    this.configUrl, { observe: 'response' });
}

In terms of use in your code, you have all of the same options I outlined before to define that value and your options object.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    This was a very educative day in regard to `HttpClient` for me. I've used in before but never needed to get deeper in the actual mechanics of it. I'm so happy I did now. – Konrad Viltersten Jun 30 '20 at 14:53