Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to send a string array in HttpClient post method in Angular 14?

I try to send a string array with HttpClient post method to my backend.

getAnnouncementsByIds(ids: string[]): Observable<Announcement[]> {
  return this.http.post<Announcement[]>(`${environment.serverUrl}${this.urlGetAnnouncementsByIds}`, ids);
}

Current error is: Http Status 400 with message "JSON parse error: Cannot deserialize value of type [Ljava.lang.String; from Integer value (token JsonToken.VALUE_NUMBER_INT);

I tried different things to make it work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • add the number array as stringified value to HttpParams
  • add a header with "Content-Type": "application/json"

i cant find a solution how to send an array as body in an Http Post request.

Can someone help me? Im sure it should be pretty easy.

>Solution :

You can make a object like this:

export interface IdList{
 ids: string[]
}

First option:

getAnnouncementsByIds(ids: string[]): Observable<Announcement[]> {
  const rq = {ids = ids} as IdList
  return this.http.post<Announcement[]>(`${environment.serverUrl}${this.urlGetAnnouncementsByIds}`, rq);
}

Second option:

getAnnouncementsByIds(ids: IdList): Observable<Announcement[]> {
  return this.http.post<Announcement[]>(`${environment.serverUrl}${this.urlGetAnnouncementsByIds}`, ids);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading