Search code examples
angularangular-httpclient

Why serializeBody method in request.ts does not convert JSON.stringify(this.body) in case string?


When i pass string value into post method, this value was not converted JSON. So We could not get this value in Controller.

I checked following code and find the reason why value was not converted.

Link

But I could not understand this behavior is correct.

apply(id: string): Observable<boolean> {
    return this.http.post<boolean>(
      REQUEST_URL + '/applicationrequest',
      id,
      HEADERS
    );
  }
@ResponseBody
    @RequestMapping(value = "/applicationrequest", method = RequestMethod.POST)
    public boolean apply(@RequestBody UUID id) {
        return usecase.apply(id);
    }

I want to know the reason why string should not be converted JSON automatically. And if way to use httpClient, I want to know the correct way to use. Thank you.

Angular Version is 8.1.0. Java is Java 8. Spring is 5.1 / Spring Boot 2.1


Solution

  • Simply because those are 2 totally different things. Angular (or the httpclient) can't know by magic what exactly the endpoint requires or expects as input.

    It's simply up to you as a developer to generate the http request that matches the server's interface.