0

how can I pass a value key from a GET url response to another GET url as a parameter in Angular component ?

The first url needs to be execute first after that only second url must get execute. Any example that can be done with or without service ?

1
  • please show what u have tried so far.. bcs question is rather unclear. Commented Jul 7, 2020 at 7:11

2 Answers 2

1
this.apiService.firstAPI().subscribe(res => {
   if(res.body){
    var id = res.body.data[0].id;
     this.apiService.SecondAPI(id).subscribe(res => {

     }
   }
 }

this way your second API will be called only when first API response comes.

as for the api service function

SecondAPI(id) {
    return this.httpService.get('api/getmyData?id='+id);
}
Sign up to request clarification or add additional context in comments.

3 Comments

suppose in response of first API I got data as { "id": 1} . Now how can I pass this this id to second API as a parameter ?
i have updated my answer, i hope i covered most of it.
Nested subscribes is not a good practice. Its theoretically OK to use it in http calls, but will cause issues when you start writing more complex RxJS scenarios. Also, the code will be more readable if you use rxjs operators.
1

The RxJS way to do this is to use switchMap

this.http.get('/url1')
.pipe(
     switchMap((res)=>{
          return this.http.get('/url2' + res.id);
     })
)
.subscribe((res)=>{
     // Do whatever
});

1 Comment

Yes , I was unaware of this. Thanks I will try

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.