1

Hi I'm new to angular hence got stuck at one place. I need to pass array as parameters to backend API backend API expects array of string as parameters

 const params = new HttpParams();
 const depKey = ['deploymentInprogress', 'deploymentMessage'];
 params.append('depKey', JSON.stringify(depKey));

 this.http.get(uri, { params: params })
      .pipe(
        take(1),
        catchError((error) => {
          return throwError(error);
        })
      ).subscribe(data => {

   })
 

The above code didn't work Need an help on this I'm not sure how we can pass array of strings as params to our backend API

6
  • What means "didn't work"? What specifically didn't work? You are not passing an array, but a JSON-String that represents your array. It's hard to give any specific answer if we don't know how your backend expectes this data. Commented Dec 13, 2022 at 7:50
  • backend expects array of string. How can we pass the params as array of string? Commented Dec 13, 2022 at 7:51
  • 1
    Your API should be POST method to receive complex values such as objects/arrays. GET method for API only supports simple value via the query string, and query params. Commented Dec 13, 2022 at 7:53
  • @YongShun ok can you provide me the exact snippet using above params? Commented Dec 13, 2022 at 7:56
  • Signature of GET says we can pass array of stings get(url: string, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: 'body'; params?: HttpParams | { [param: string]: string | string[]; }; Commented Dec 13, 2022 at 7:57

3 Answers 3

3

You can do this:

const params = new HttpParams();
 const depKey = ['deploymentInprogress', 'deploymentMessage'];
 params.append('depKey', depKey.join(', ');

 this.http.get(uri, { params: params }).....
Sign up to request clarification or add additional context in comments.

2 Comments

The term "body" is misleading here. A GET requests isn't supposed to have a body and angular doesn't give you the option to pass one with .get().
Yes you are absolutely right, I copied and pasted without realizing it was a get.
1

You can send the array values individually with the same parameter name, then api can read the individual values!

 const params = new HttpParams();
 const depKey = ['deploymentInprogress', 'deploymentMessage'];
 depKey.forEach((item: any) => {
     params.append('depKey', item);
 });

 this.http.get(uri, { params: params })
      .pipe(
        take(1),
        catchError((error) => {
          return throwError(error);
        })
      ).subscribe(data => {

   })

Comments

0

You need to know what the specification of the backend API is. Without the specification it's just guessing how to get this to work.

Most of the times it will be encoded in the body and that can be done in may forms, for example:

{ 
   "key": "value",
   "items": [
      {"aaa":"bbb","ccc":"dddd"},
      {"aaa":"fff","ccc":"hhhh"}
   ]
}

but note: this is just one way of doing it!

If you can share your specifications, we can help you.

Maybe you have a postman example on how to call the endpoint?

Comments

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.