0

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.

chris
  • 2,490
  • 4
  • 32
  • 56
  • btw, `this.httpConfig.getHeaderOptions()` is the header for the request, which is correct since I'm using it in another http request that does not require any parameter in the url – chris Aug 07 '18 at 16:56
  • 1
    Why oh why don't you simply read the documentation? https://angular.io/guide/http#url-parameters, https://angular.io/api/common/http/HttpClient#get. get() expects **2** arguments, not 3. – JB Nizet Aug 07 '18 at 17:01

1 Answers1

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