This is the GET mapping in my backend:
@GetMapping(value = "search")
public List<Cat> search(@RequestBody CatDto catDto) {
return catService.search(catDto);
}
I want to send a GET request to get a list back in Angular HttpClient
. I know that I can't send a request body with my GET request and I did this in my component.ts
:
search(cat: Cat): Observable<Cat[]> {
let params = new HttpParams();
params = params.append('name', cat.name);
params = params.append('rating', (cat.rating).toString());
params = params.append('birtday', (cat.birthday).toLocaleDateString());
return this.httpClient.get<Cat[]>(this.messageBaseUri+'/search', {responseType: 'json', params});
}
Of course I am getting:
Required request body is missing
From my backend. Can I do this somehow without changing my backend or do I have to make my backend endpoint look like this:
@GetMapping(value = "search")
public List<Cat> search(@RequestParam String name,
@RequestParam Integer rating,
@RequestParam Date birthday) {
return catService.search(name, rating, birthday);
}
GET requests usually do not have an HTTP body.
Changing your Back-End to accept request params rather than a request body seems like the cleanest solution. However, you can simply change it like this:
@GetMapping(value = "search")
public List<Cat> search(CatDto catDto) {
return catService.search(catDto);
}