5

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

2 Answers 2

4

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0
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()}`);
  }

1 Comment

I suppose the above is just a typo and you are using the correct variable, ie ids[k] instead of relationIds[k]?

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.