chris
chris

Reputation: 2810

How to include parameter in HTTP request with Angular 6

I need to call the following endpoint https://sample_url?id=12345 from an Angular service.

UserService

getUsersByGroupID(id): Observable<User[]> {
 var url = 'https://sample_url';

 var params = new HttpParams();
 params = params.append('id', id);

 this.data = this.http.get<User[]>(url, {params: params}, this.httpConfig.getHeaderOptions()).pipe(catchError(this.handleError('getData', [])));
 return this.data;
}

In one of my components I should do something like...

  this.userService.getUsersByGroupID(sys_id).toPromise().then(users => {
   this.users = users as User[]
}

but i'm not sure how to include the query parameter ?id=correctly. I tried looking at How to pass url arguments (query string) to a HTTP request on Angular but didn't help much.

Thanks.

Upvotes: 0

Views: 2624

Answers (1)

K. Ayoub
K. Ayoub

Reputation: 426

getUsersByGroupID(id: number): Observable<User[]> {
    return this.http.get<User[]>("https://sample_url?id=" + id)
        .pipe(
             map(response => {
                 return response;
    }));
}

Upvotes: 1

Related Questions