Solved

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

Posted on 2016-10-03
5
14 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
  • 3
  • 2
5 Comments
 
LVL 35

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 35

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 35

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now