This is my array of actors:
['Elvis', 'Jane', 'Frances']
How to pass this array within a query string in HttpClient?
I tried to use:
1)
let params = new HttpParams();
params = Params.append('actors[]', ['Elvis', 'Jane', 'Frances']);
this.http.get(url, { params: Params });
let params = new HttpParams().set('actors[]', ['Elvis', 'Jane', 'Frances']);
this.http.get(url, { params: Params });
let Params = new HttpParams();
Params = Params.append('actors[]', 'Jane');
Params = Params.append('actors[]', 'Elvis');
Params = Params.append('actors[]', 'Frances');
this.http.get(url, { params: Params });
1st and 2nd snippets don't work because of TypeScript error:
[ts] Argument of type 'string[]' is not assignable to parameter of type 'string'.
3rd snippet sends only one item 'actors[]': 'Frances'
actors=Elvis,...query? A solution from the answer should be used then. I personally had to use npmjs.com/package/qs package for params because it worked better for arrays and nested objects with my Express backend.?actors[]=Jane&actors[]=Elvis&actors[]=Frances. I'm guessing that your backend is only seeing the first one because it does not recognize this format for query string arrays, as Estus Flask mentioned.