Guru Cse
Guru Cse

Reputation: 3295

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

Upvotes: 5

Views: 7319

Answers (2)

Nazar Kohut
Nazar Kohut

Reputation: 41

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());

Upvotes: 4

Guru Cse
Guru Cse

Reputation: 3295

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()}`);
  }

Upvotes: 0

Related Questions