Solved

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

Posted on 2016-10-03
5
23 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
Java Timer (static) 9 43
difference of if loops 23 49
by zero exception 10 39
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

776 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