Handle special characters in com.ning.http.client RequestBuilder

Hi,
I am using the RequestBuilder class like :
RequestBuilder rb = this.getRequestBuilder();
        rb.setUrl(endpoint);
        rb.setBody(this.gson.toJson(msg));
        rb.setMethod("POST");
        return (String)asyncHttpClient.executeRequest(requestBuilder.build(), new AsyncCompletionHandler() {
            public String onCompleted(Response response) throws Exception {

Open in new window

The msg is contains elements like smileys etc... ie. special characters...
Now i am catching the result of this post operation in a controller method like :
 @RequestMapping(value="/test", method = RequestMethod.POST)
    public void test(HttpServletRequest request, HttpServletResponse response, @RequestBody  String json)
    {

Open in new window


The json that i get doesnt have smilets or special characters they have been replaced by ?
My controller is also getting some ajax calls containng special characters... so The spring application is setup correctly to handle smileys etc.. Only with this Request builder object when i am making a call on the controller end the special characters are replaced....  
Also i tried setting UTF-8 encoding : requestBuilder.setBody(this.gson.toJson(message).getBytes("UTF-8"));
This produces characters like : 😃  comment  Which was actually a 😃  comment

How do i make it handle special characters what could be wrong.

Thanks
Rohit BajajAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Ok, so it's hard to say now whether the problem is the sending side or the receiving side, it could be either.

Are you able to use something like Wireshark to trace the traffic between sender and receiver so that we can determine which end is causing the issue?

I think the problem maybe around the request that is being sent. I was hoping that "setBodyEncoding" would actually set a request header telling the receiving end (Spring) what encoding to use to "decode" the request, otherwise Spring uses a different default encoding. But possibly that method had just told RequestBuilder how to obtain the bytes for the string that you are passing it (similar to what you did manually).

The javadoc for that library is not great, but maybe you could try explicitly setting the "Content-Type" header to include the correct charset, such as      setHeader("Content-Type", "application/json; charset=UTF-8") or     setHeader("Content-Type", "text/plain; charset=UTF-8")
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I've never used that library, but on a quick look at the doco there is a setBodyEncoding method. Have you tried this...

rb.setBodyEncoding("UTF-8");

Open in new window

0
 
Rohit BajajAuthor Commented:
yes this does the same thing where i set bytes in utf 8.
This produces characters like : 😃  comment  Which was actually a 😃  comment
previoualy it was giving ? now this other special character
0
 
Rohit BajajAuthor Commented:
HI,
Setting setHeader("Content-Type", "application/json; charset=UTF-8") worked.
Also i noticed that in wireshark ? was only getting passed. So the requestBuilder internally was encoding it in some other format apart from Unicode and sending the wrong data ....
That means the seting header apart from being telling the recipient that data is in UTF-8 format is also being used by requestBuilder to encode the data in proper format.. otherwise it encodes it in wrong format and sends it to the server....
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Glad it's working now!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.