Home > Software design >  Getting a 400 Bad Request when Angular HttpClient posts a Set<> to a SpringBoot endpoint
Getting a 400 Bad Request when Angular HttpClient posts a Set<> to a SpringBoot endpoint

Time:09-15

I am exposing an endpoint that accepts a Set<> as a @RequestBody this way :

public @ResponseBody ResponseEntity<Response> addTeamOwner(@RequestParam("teamName") String teamName, @RequestBody Set<String> emails, HttpServletRequest request){...}

And my Angular frontend is calling this endpoint like this :

let params = new HttpParams().set('teamName', teamName);
let url = `${UrlManager.TEAMS}/addOwners?${params.toString()}`;
this.httpClient.post<any>(url, emails);

For some reason I'm getting 400 Bad Request : HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: 'Bad Request', url: 'http://localhost:4200/api/teams/addOwners?teamName=DEMO_TEAM', ok: false, …}

It seems that the Set that Angular is sending is not accepted by the backend because when I change to an Array everything works fine !

FYI, my API is SpringBoot and my frontend is Angular.

CodePudding user response:

Actually it is not possible to serialize data sent within Set because the data are not stored as properties.

The solution was to convert the set to an array this way :

this.httpClient.post<any>(url, [...emails]);

and the backend is able to deserialize it as a Set correctly.

  • Related