39

When sending a Base64 encoded string as header using Http, I am getting error response as

Unexpected char 0x0a at 28 in header value: I99Uy+HjG5PpEhmi8vZgm0W7KDQ=

Usage :

String encodedHeader = Base64.encodeToString(value.getBytes(), Base64.DEFAULT); header.put("auth", encodedHeader);

priyankvex
  • 5,760
  • 5
  • 28
  • 44

1 Answers1

88

0x0a is a newline character which is forbidden in a header. Solution would be to make sure that these characters are stripped off before sending the encoded value as header.

Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); this avoids wrapping with a platform specific newline character(s).

priyankvex
  • 5,760
  • 5
  • 28
  • 44
  • 7
    If you use Okio’s ByteString, base64 is easier: `encodedHeader = ByteString.encodeUtf8(value).base64();` – Jesse Wilson Mar 26 '17 at 12:50
  • @priyankvex thanks for the solution. It works like charm. But I didn't understand " this avoids wrapping with a platform specific newline character". Can you please make me understand this. Thanks :) – Ashutosh Chamoli Apr 12 '18 at 07:33