?
Solved

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

Posted on 2016-10-03
5
Medium Priority
?
48 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 2000 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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 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