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 :)

Comments

Post a Comment

Popular posts from this blog

'jasypt.encryptor.password' or one of ['jasypt.encryptor.privateKeyString', 'jasypt.encryptor.privateKeyLocation'] must be provided for Password-based or Asymmetric encryption

Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - Spring Batch

java.security.spec.InvalidKeySpecException: Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys