Expand my Community achievements bar.

URLDecoder throwing exception of illegal hex character in escape pattern (%) for a string response

Avatar

Level 2

We are consuming response from 3rd party service which has product related information. 

Below is the code snippet we are using:

 

String newstr = responseData.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
newstr = responseData.replaceAll("\\+", "%2B");
log.debug("newstr val is : "+newstr);
str0 = URLDecoder.decode(new String((responseData).getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");
log.debug("Service response str0 : "+str0);
str2 = URLDecoder.decode(new String((newstr).getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");
log.debug("Service response str2 : "+str2);
str3 = URLDecoder.decode(new String((responseData).getBytes("UTF-8"), "ISO-8859-1"), "UTF-8");
log.debug("Service response str3 : "+str3);
str1 = URLDecoder.decode(new String((newstr).getBytes("UTF-8"), "ISO-8859-1"), "UTF-8");
log.debug("Service response str1 : "+str1);
//#1
//responseData = str2;
//#2
responseData = responseData;

 

Earlier I was sending the string - responseData directly, but it created an issue with translating some special characters of fr,de,it languages. Then I tried decoding it using URLDecoder, I know the encoding is supposed to be done in UTF-8. 

But if I use str0, str1, str2, str3 I am able to see that we can decode the response string and special characters can be translated properly but when we have some product title having '%' special character in it, the code breaks giving following exception - URLDecoder throwing exception of illegal hex character in escape pattern (%) as shown in the attached image.

 

Is there any way I could resolve both the problems without manually altering the response string?

2 Replies

Avatar

Community Advisor

@avni27 - I don't think the issue is with URL Decoder. Can you share the illegal hex character being flagged by the decoder?

 

Just earlier today I used it to translate the below - 

Identity%20Mulitple%20Profiles_FR.pdf to Identity Mulitple Profiles_FR.pdf via 
fileName = URLDecoder.decode(fileName, "UTF-8");

In an URL the % has to be followed by a valid pair of hexadecimal digits.
If you want to use a reserved character like '%' you have to use percent encoding. 

https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding

 

Avatar

Level 2

Hi @Rohan_Garg ,

Thanks for the comment and the URLDecoder.decode() method works fine for translated characters used in fr/de/it langugaes but I cannot use it with content having '%' symbol in it.

It seems the content is not encoded completely, while there are strings like '™' for trademark  and ' ' present in the response the character '%' is not encoded. 

So I was looking for something that would tell the client to print '%' as it is or is there any need to modify request/response headers to tell browser to use utf-8 to display the data.