Character Encoding in POST JSON Request
Asked Answered
B

6

20

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 ?

Billups answered 14/10, 2015 at 14:14 Comment(8)
How do you send the request?Syndetic
try set your Content-Type to application/json; charset=UTF-8 and try it again.Lubbock
@Syndetic I send my request via Postman Rest Client. The request is mentioned above in the question.Billups
@Lubbock I tried it. But no luck :(Billups
results in some junk characters "é¢é£å½äºè" how do you observe that? (it could simply be your output that uses the wrong encoding)Syndetic
@Syndetic I observed it through my log, which is UTF-8 encoded. Also I am character encoding my response to UTF-8 too. That you could see in Produces Annotation that I used in my service class.Billups
Hello @Elite209, If you just found the solution please share with me too. I got stuck in it too. ThanksSalamis
Hey @Sahil: I solved the problem for myself and posted the solution with a picture. For me, the accept-encoding setting was causing this behaviour. Hope this helps you, too!Thicken
I
28

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.

Inspired answered 29/1, 2016 at 9:48 Comment(0)
T
10

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.

Thicken answered 15/12, 2018 at 17:48 Comment(4)
what tool/browser did you use to edit the headers like this?Millian
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.Thicken
Thanks @Matt! I was able to find it after alot of searching!!!Millian
It is a valid request header to inform the server, only that value is wrong in your case.Diastole
E
2

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.

Eisler answered 24/9, 2020 at 13:35 Comment(0)
G
1

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

Genny answered 4/9, 2019 at 4:36 Comment(0)
C
1

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

response.setCharacterEncoding("utf-8");
Cantata answered 19/2, 2020 at 14:28 Comment(0)
C
0

A hack is to make use of the Pre-request Script in Postman.

Paste the following in the Pre-request Script section. This will transform all our params in the Request Url to be encoded. Thus all the + sign would be encoded to %2B

var querycount = pm.request.url.query.count();
for(let i = 0; i < querycount; i++) {
  pm.request.url.query.idx(i).value = encodeURIComponent(pm.request.url.query.idx(i).value);
}
Cobble answered 6/7, 2022 at 17:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.