'pass array in query params Angular HttpClient
How form a URL with array of query params with Angular HttpClient
input
ids: string[] = ["1","2","3"]
output
eg: https://localhost:8080/cinemas?ids=1&ids=2&ids=3
Solution 1:[1]
import { HttpClient, HttpParams } from '@angular/common/http';
constructor(private httpClient: HttpClient) {}
url = https://localhost:8080/cinemas
public getCinemas(ids: string[]) {
let queryParams = new HttpParams();
for (let k = 0; k < ids.length; k++) {
queryParams = queryParams.append('ids', ids[k]);
}
return this.httpClient.get(this.url + `?${queryParams.toString()}`);
}
Solution 2:[2]
Simply use appendAll() and pass there {"param": Array}
let queryParams = new HttpParams();
queryParams = queryParams.appendAll({'ids': ids});
return this.http.get(backendUrl + '/cinemas?' + queryParams.toString());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
