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:
- 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.
CodePudding user response:
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);
}
CodePudding user response:
You can wrap the array with an object
getAnnouncementsByIds(ids: string[]): Observable<Announcement[]> {
// ids -> { ids }
return this.http.post<Announcement[]>(`${environment.serverUrl}${this.urlGetAnnouncementsByIds}`, { ids });
}
So your backend will get this value
{
ids: [...]
}
CodePudding user response:
look at the uploaded screenshot