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 });
2)
let params = new HttpParams().set('actors[]', ['Elvis', 'Jane', 'Frances']);
this.http.get(url, { params: Params });
3)
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 });
1 and 2 doesn't work so as typescript gives:
[ts] Argument of type 'string[]' is not assignable to parameter of type 'string'.
3 sends only one item 'actors[]': 'Frances'
This worked for me.
OR
Here is a simple way to do it:
I'm using
URLSearchParams
instead of HttpParams.With URLSearchParams you need to stringify your array to set it to your Params "Key-Value-Store".
I think this should work on the same way with HttpParams because both using a Key-Value mapping in the set method so give it a try.
I hope this can help you.
UPDATE:
with
RequestOptions
you can also edit Header and other request options.Works on Angular 6.0.6:
}
Result:
I think the best way is to add them to parameters as a
string
and have your back-end convert it back to anarray
orlist
.