Solved

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

Posted on 2016-10-03
5
40 Views
Last Modified: 2016-10-04
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
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 41827324
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
 

Author Comment

by:Rohit Bajaj
ID: 41827410
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
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 41827457
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
 

Author Comment

by:Rohit Bajaj
ID: 41827578
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
 
LVL 36

Expert Comment

by:mccarl
ID: 41827962
Glad it's working now!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question