Passing parameter array list on http native get method typescript

Viewed 2134

I am trying to pass array list parameter . parameter codeList contains array like

[
  {
    "projCode": "11-1115",
    "cblTagNo": "571_GE001-RC1"
  },
  {
    "projCode": "11-1115",
    "cblTagNo": "571_GE001-S"
  }
]

But I am facing the error "invalid params object, needs to be an object with strings". May I know how to pass the obj array. Here is my http call method.

checkCableTagList(codeList: String[]){
  this.http.setDataSerializer('json');
  console.log(JSON.stringify(codeList));
  return this.http.get('http://hostname/cable/v2/cableschedule/key/', codeList, {});
}

I have tried this way, It didn't work as well.

return this.http.get('http://hostname/cable/v2/cableschedule/key/', JSON.stringify(codeList), {});
1 Answers

You can make use of HttpParams to send this array.

   let httpParams: HttpParams = new HttpParams();
   codeList.forEach(code => httpParams = httpParams.append('Projects', code ));

 //Http request-
 return this.http.get('http://hostname/cable/v2/cableschedule/key/', {
   params: HttpParams 
 }).subscribe(
   (response) => //some manipulation with response 
 );
Related