I have endpoint, which returns an endpoint url.
http://example.com/get-endpoint
I want to invoke first GET and then using the result url invoke the second GET. How can I achieve this using chained observables and RxJS? I don't know how to pass url of the first GET to the second GET
CodePudding user response:
You may use flatMap
:
this.http.get('http://example.com/get-endpoint')
.pipe(
flatMap(endpoint => this.service.getObservable(result))
)
.subscribe(result => console.log(result));
Take a look at the documentation here
CodePudding user response:
Use RxJS's switchMap operator.
// your service...
getData(){
return this.httpClient.get('http://example.com/get-endpoint', { responseType: "text"}).pipe(
switchMap(url => this.httpClient.get(url))
);
}
If your endpoint does not return a string but JSON, you should remove the { responseType: "text"}
option object, and pass the right property to the inner this.httpClient.get
call.