Pallavi Goliwale
Pallavi Goliwale

Reputation: 111

Non ascii characters in URL param in camel

I am using graph API of Facebook and I call it through camel framework. My query has non ASCII characters (e.g. küçük). I am getting the following exception:-

Cause: 
org.apache.commons.httpclient.URIException: Invalid query
at org.apache.commons.httpclient.URI.parseUriReference(URI.java:2049)
at org.apache.commons.httpclient.URI.<init>(URI.java:147)
at org.apache.commons.httpclient.HttpMethodBase.getURI
at org.apache.commons.httpclient.HttpClient.executeMethod
at org.apache.commons.httpclient.HttpClient.executeMethod
at org.apache.camel.component.http.HttpProducer.executeMethod
at org.apache.camel.component.http.HttpProducer.process
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:122)

Does camel support non ASCII characters in URI? If not, what other things can be done?

example URL: https://graph.facebook.com/?ids=http://www.example.com/küçük

Upvotes: 10

Views: 2765

Answers (3)

Sid
Sid

Reputation: 4997

So this is what we were able to do to fix the issue.

In Apache Camel, the HTTP_URI component does not accept any special characters, even after encoding them. This is a bug in Camel which is not yet closed.

Fortunately for us, the special characters would only appear in the Query String of the URL and not the main URI part. Camel provides another component HTTP_QUERY, which can successfully parse and understand encoded UTF-8 characters. By setting it in the Header, we were able to get rid of the issue.

So basically first we encode the URL to UTF-8 and then set the HTTP_QUERY value as query string. This worked like charm. e.g. (Scala)

.setHeader(Exchange.HTTP_QUERY, _.in[HttpRequest].uri.split(?).head)
.setHeader(Exchange.HTTP_URI, _.in[HttpRequest].uri)

Upvotes: 0

Prasanna Kumar H A
Prasanna Kumar H A

Reputation: 3431

Use encodeURIComponent(url) it will work

Upvotes: 0

djb
djb

Reputation: 1674

"URL encoding replaces non ASCII characters with a "%" followed by hexadecimal digits." (more info here)

You could try this:

URL url = new URL(uri.toASCIIString()); 

or maybe

String xstr = URLEncoder.encode("维", "utf-8");

Upvotes: 4

Related Questions