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.

2
  • 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 Commented Aug 7, 2018 at 16:56
  • 1
    Why oh why don't you simply read the documentation? angular.io/guide/http#url-parameters, angular.io/api/common/http/HttpClient#get. get() expects 2 arguments, not 3. Commented Aug 7, 2018 at 17:01

1 Answer 1

1
getUsersByGroupID(id: number): Observable<User[]> {
    return this.http.get<User[]>("https://sample_url?id=" + id)
        .pipe(
             map(response => {
                 return response;
    }));
}
Sign up to request clarification or add additional context in comments.

2 Comments

You may add some description to your answer to explain what changes have you made.
Can you describe this solution ? and if it's really working, can you please give us the Output ? Thank you! It can help a lot of junior developers :)

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.