{"id":3543,"date":"2011-04-11T16:09:06","date_gmt":"2011-04-11T10:39:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3543"},"modified":"2011-04-11T16:10:37","modified_gmt":"2011-04-11T10:40:37","slug":"iso-8859-based-url-encoding-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/iso-8859-based-url-encoding-in-grails\/","title":{"rendered":"ISO-8859 Based URL Encoding in Grails"},"content":{"rendered":"<p>The web application we are developing at the moment interacts with quite a few third party services via REST calls. Most of the third party services use UTF-8 encoding and there was no issues with using the URLCodec, that grails provides out of the box. However, one of the applications our application interacts with, was using ISO-8859-1 encoding. This meant that the use of encodeAsURL() provided by grails resulted in misinterpretation of data sent by us to those services. (The audience to which the site caters has languages extensive use of characters like \u00e5, \u00c5, \u00e4, \u00c4 etc).<\/p>\n<p>On going through the URLCodec which grails provides out of the box, we found that the encoding is UTF-8 by default and can be overridden only by the encoding flag in the incoming HTTP request. However, the REST calls we make were outside of HTTP requests and the encoding for the rest of the application is UTF-8.<\/p>\n<p>Therefore, we ended up writing a custom IsoURLCodec, which could be used while dealing with this particular service, which needed ISO-8859-1 encoding of URLs as opposed to UTF-8 used by all other services.<\/p>\n<p>[java]<br \/>\nclass IsoURLCodec {<br \/>\nstatic encode = { obj -&gt;<br \/>\nURLEncoder.encode(obj.toString(), &quot;ISO-8859-1&quot;)<br \/>\n}<br \/>\nstatic decode = { obj -&gt;<br \/>\nURLDecoder.decode(obj.toString(), &quot;ISO-8859-1&quot;)<br \/>\n}<br \/>\n}<br \/>\n[\/java]<\/p>\n<div>Now, we could make use of calls like<\/div>\n<p>[html]<br \/>\n&quot;${&#8216;&lt;span style=&quot;font-family: sans-serif;&quot;&gt;Philippe P\u00e9tain&lt;\/span&gt;&#8217;.encodeAsIsoURL()}&quot;<br \/>\n[\/html]<\/p>\n<div>to encode the name to the corresponding ISO-8859-1 encoded URL.<\/div>\n<div>Hope this helps.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The web application we are developing at the moment interacts with quite a few third party services via REST calls. Most of the third party services use UTF-8 encoding and there was no issues with using the URLCodec, that grails provides out of the box. However, one of the applications our application interacts with, was [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[7],"tags":[550,551,4840,549],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3543"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3543"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3543\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}