DEV Community

Cover image for How to set query parameters in angular rest call
Adrian Matei for Codever

Posted on • Updated on • Originally published at codever.dev

How to set query parameters in angular rest call

In Codever we use extensively the Angular Http Client to make REST calls against a NodeJs/ExpressJS API - source code on Github.

In the following snippet you can see hot to set http query parameters to the rest api calls.

Use the HttpParams class with the params request option to add URL query strings in your HttpRequest:

  getFilteredPersonalBookmarks(searchText: string, limit: number, page: number, userId: string, include: string): Observable<Bookmark[]> {
    const params = new HttpParams()
      .set('q', searchText)
      .set('page', page.toString())
      .set('limit', limit.toString())
      .set('include', include);
    return this.httpClient.get<Bookmark[]>(`${this.personalBookmarksApiBaseUrl}/${userId}/bookmarks`,
      {params: params})
      .pipe(shareReplay(1));
  }

Enter fullscreen mode Exit fullscreen mode

Shared with ❤️ from Codever. 👉 use the copy to mine functionality to add it to your personal snippets collection.

Top comments (0)