How to enable utf-8 request body in spring-boot?

Viewed 509

I have a spring-boot project (spring-boot version: v2.3.0.RELEASE). In my api I wish to receive String body, that I will then handle as I please. Problem is, that if this String body has an utf-8 character in it, it somehow changes the string:

    @RequestMapping(value= "/target", method = RequestMethod.POST)
    public List<Target> createTarget(@RequestBody String body) {
       return this.createTargets(body);
    }

If there's no utf-8 characters in the string (which is json btw), then when I debug the value of body variable, it looks ok. But when there are utf-8 characters that I try to post to the api, then what happens is that last character of that whole body string is stripped off. I don't know why is this happening? Tried some fixes I found over stackoverflow but no help so far =/. My case is that I send a json string that contains an array. So the last square bracked ] is being stripped off and I have no clue why.

2 Answers

Was able to fix this. Turns out it wasn't necessarily just a problem with spring-boot. In short, I removed this header from the request and it started working nicely:

'Content-Length': data.length,

So for some reason with utf-8 characters in the string, the data.length was probably off by one? Or it was something that spring-boot took too seriously, and therefore cut the last character from the string.

Related