0

I would like to do a multi call using 'HttpClient'. Something similar that I used to use with axios.

Using axios with Vue:

return axios.all([
      axios.get('/friends/name'),
      axios.get('/family/name'),
      axios.get('/collegue/name'),
])

Trying with angular:

return this.http.all([
      this.http.get('/friends/name'),
      this.http.get('/family/name'),
      this.http.get('/collegue/name'),
])

error TS2339: Property 'all' does not exist on type 'HttpClient'

2
  • 2
    You should be using forkJoin() Commented Nov 28, 2019 at 13:43
  • here Commented Nov 28, 2019 at 13:53

3 Answers 3

1

Try with forkJoin like this:

 ngOnInit() {    
    const request1 = this.http.get('/friends/name');
    const request2 = this.http.get('/family/name');
    const request3 = this.http.get('/collegue/name');

    forkJoin([request1, request2, request3]).subscribe(data => {
      this.response1 = data[0];
      this.response2 = data[1];
      this.response3 = data[2];
    });
  }
Sign up to request clarification or add additional context in comments.

2 Comments

That is right but why use axios in angular? It also works perfectly with httpClient
Well I thought axios is the name of his service where http calls are written, had no idea it's a library. Thanks for pointing it out.
1

You have to use a forkJoin operator and subscribe to the data as below

forkJoin([
  this.http.get('/friends/name'),
  this.http.get('/family/name'),
  this.http.get('/collegue/name'),
 ])

Comments

1

When making a request using the HttpClient, it will return an rxjs Observable which you will have to subscribe to in order for it to make the request. You should look into one of these operators from rxjs, depending on your use case.

You could for example use the merge operator like this:

const requests = merge(
    this.http.get('/friends/name'),
    this.http.get('/family/name'),
    this.http.get('/collegue/name')
);
requests.subscribe(console.log); // Executes the requests and logs them as they complete

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.