Reputation: 699
I have a service that connects to 2 different API endpoints, this one gets the user's id and a JWT token:
@Injectable()
export class UserService {
appLogin = 'http://url.for.the.login.api';
//returns id and token
userData = 'http://url.for.the.user_info.api';
//returns user info
constructor(private http: HttpClient) { }
getAll() {
return this.http.get<User[]>(`${this.appLogin}`);
//gets id and token, can display values in the template like this {{currentUser.id}}
}
}
Then I need that another method uses the id and the token returned to use those values to make another GET
request, like this:
http://url.for.the.user_info.api/userID?token=token_string
I have this:
getInfo(id: number, token: string) {
let userData = this.http.get<User[]>(`${this.appLogin}` + id + '?token=' + token);
return this.http.get<User[]>(`${userData}`);
}
But I'm lost with this, thank you for your help.
Upvotes: 0
Views: 36
Reputation: 18271
You can use the switchMap
function, like so:
return this.http.get<User[]>(`${this.appLogin}` + id + '?token=' + token)
.pipe(switchMap(userData) => this.http.get<User[]>(`${userData}`))
You may need to add the following import statement for switchMap
:
import { switchMap } from 'rxjs/operators';
By piping the original request through switchMap
, we can create a new Observable
Upvotes: 2