Wednesday, August 13, 2014

Illegal character(s) in message header value

Recently was trying to consume a Rest Web Service by passing AES256 encrypted values in header Params and hit the above exception.

java.lang.IllegalArgumentException: Illegal character(s) in message header value: 

After a little bit of resaerch i found that it is a bug with java while using BASE64 Encoded String.If the string length exceeds 76 characters, the Base64Encoder adds a "\n" character. While trying to sent it in header it fails complaining about the "\n" in the Base64 encoded string.

This is actually a bug with JAVA

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6459815

The fix is 

String encodeMe = m_username + ":" + m_sToken;
        
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Encoded = encoder.encode(encodeMe.getBytes());
        //!Important! - Get rid of any newline characters erroneously
        //              added by the Base64Encoder
        base64Encoded = base64Encoded.replaceAll("\n", "");
        
        basicAuthCredentialsBase64 = base64Encoded;
The line highlighted in red will do the trick. Simply replace all \n with space and issue resolved.


This Stack Overflow post also speaks on same

Illegal Character in header

Happy Coding :)

No comments:

Post a Comment