Character Encoding in POST JSON Request

100,546

Solution 1

Set the content-type to:

"application/json;charset=UTF-8" 

when sending the post request in the application you are using. You can find "content-type" in the Header of the URL in that application.

Solution 2

None of the answers here worked for me.

My content-type was already set to "application/json;charset=UTF-8", but the accept-encoding setting in my header was causing the error:

Disable the accept-encoding setting under Headers:

enter image description here

When I deactivated the last line above, everything worked great! Hope that helps someone.

Solution 3

I think you need to use json_encode() options like this line

json_encode($data,JSON_UNESCAPED_UNICODE);

Postman returns the message in your language format.

Solution 4

Try this

@RequestMapping(value = "/play", method = RequestMethod.POST, produces={"application/json; charset=UTF-8"})

Set produces={"application/json; charset=UTF-8"} as above, to your @RequestMapping

Solution 5

I had a similar problem and solved it bu setting HttpServletResponse instance's character encoding to utf-8 :

response.setCharacterEncoding("utf-8");
Share:
100,546
Vis
Author by

Vis

Updated on July 09, 2022

Comments

  • Vis
    Vis almost 2 years

    I am sending a POST JSON Request to my application.

    POST /CharSetTest/Test HTTP/1.1
    Host: localhost:8090
    Content-Type: application/json
    Cache-Control: no-cache
    Postman-Token: 1637b92b-5896-4765-63c5-d04ad73ea9f1
    
    {
      "SampleRequest": {
        "FullName": "関連当"
      }
    }
    

    My CXF JAXRS Consumer is defined as below.

    @POST
    @Produces("application/json; charset=UTF-8")
    @Consumes("application/json; charset=UTF-8")
    public Response testCharSet(@Encoded String jsonBody);
    

    But the Japanese Character (関連当) that I sent as POST request is not encoded and results in some junk characters "é¢é£å½äºè"

    Using SoapUI results in "?????" characters.

    This Junk characters differs from client to client from where I hit the request. How Could I encode my POST Request ?

  • Mohammad
    Mohammad almost 4 years
    what tool/browser did you use to edit the headers like this?
  • Matt
    Matt almost 4 years
    hi @Mohammad: This screenshot is from postman, which is a popular app for sending requests (among other things). It has a generous free tier, too.
  • Mohammad
    Mohammad almost 4 years
    Thanks @Matt! I was able to find it after alot of searching!!!
  • Kanagavelu Sugumar
    Kanagavelu Sugumar almost 3 years
    It is a valid request header to inform the server, only that value is wrong in your case.