0

I am developing an Angular 8 application. I am trying to implement an HttpClient get request according to the documentation: https://angular.io/api/common/http/HttpClient.

So globally I am trying to use a get(url: string, httpHeader: HttpHeaders, observe: HttpObserve, params : HttpParams) request, but it seems that HttpObserve disappeared. Can you help me?

2
  • 2
    can you please add a minimum reproducible example? thanks! Commented Nov 3, 2019 at 10:23
  • @alt255 It's been pointed out to me that the previous comment may be perceived as rude. Based on that, I want to apologize for the clumsy wording and emphasize that it wasn't my intention. Is the thought behind my comment better apparent now or would you prefer to have it explain in more detail? Commented Jun 30, 2020 at 10:45

2 Answers 2

0

Try to use it like this:

this.http.get('URL', { observe: 'response' });
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for your answer and comment. As I want to add additional code, I answer the question rather than add a comment. My main intention doing such a request is to add HttpParams (or rather an object representing all additional parameters to my get request) to the request. Here is the service method:

  getListProduitImmobilierDTO(pagesize: number, page: number, params: HttpParams): Observable<ProduitImmobilierDTO[]> {
    const headerDict = {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      'Accept-Charset': 'charset=UTF-8',
      'Access-Control-Allow-Headers': 'Content-Type'
    };
    return this.http.get('/api/produitimmobilier/all/' + pagesize + '/' + page, new HttpHeaders(headerDict), { observe: 'response' }, params).pipe(map((jsonArray: any) => jsonArray.map((jsonItem: object) => ProduitImmobilierDTO.fromJson(jsonItem))));
  }

As you can see yazantahhan, I added { observe: 'response' } in place of HttpObserve, and it indicates the following error:

Expected 1-2 arguments, but got 4.

Sorry alt255, but I can't provide a reproducible example as it is a request and it needs a server to respond to the request. What I can tell you is the following method works well:

  getListProduitImmobilierDTO(pagesize: number, page: number): Observable<ProduitImmobilierDTO[]> {
    const headerDict = {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      'Accept-Charset': 'charset=UTF-8',
      'Access-Control-Allow-Headers': 'Content-Type'
    };

    const requestOptions = {
      headers: new HttpHeaders(headerDict)
    };
    return this.http.get('/api/produitimmobilier/all/' + pagesize + '/' + page, requestOptions).pipe(map((jsonArray: any) =>jsonArray.map((jsonItem) => ProduitImmobilierDTO.fromJson(jsonItem))));
}

4 Comments

This isn't an answer, it still has errors. Expected 1-2 arguments, but got 4. - because you're passing the three options (headers, observe, params) as three separate arguments to .get, rather than putting them into one options object parameter as required.
@jonrsharpe Ah, now I realize which comment you referred to. It's definitely dangerous to refer to someone as noob. There's a risk of sounding condescending and as an experienced dev, it's my responsibility to make others feel welcome. Or else, I'm a noob, myself (condescendingly used in this context, which is fine because itäs directed towards myself). Although the comment original comment has vanished so I guess someone flagged it. Be as well.
@jonrsharpe It's been removed and I can't see it anymore. I'd like to ask about it on ELL for a better formulation for the future. Do you think you could get it back (being a high score user, that is)?
@jonrsharpe I've found an un-uppdated page with that comment and asked about it on the ELL. I sensed it might to do with the word ignorant, in fact. Interestingly, in both my my languages, that word sounds condescending but I've been informed that in English, it only means unaware of or not knowing and suggested to use it as a substitute for you don't know. Perhaps I'm about to learn something new from ELL. Either way, I'm sorry for the disturbance, appreciative for the info and grateful for the support.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.