Retrofit: Unexpected char 0x0a at 28 in header value

Created on 1 Oct 2015  路  6Comments  路  Source: square/retrofit

This is my header value: I99Uy+HjG5PpEhmi8vZgm0W7KDQ=

When I try to pass that value via parameter, retrofit is throwing an error.
I/: Failure: Unexpected char 0x0a at 28 in header value: I99Uy+HjG5PpEhmi8vZgm0W7KDQ=
But when I add the same header value as hard-coded string in API interface class, it works.

This works:

@Headers({
"Content-Type: application/json",
"app-key: I99Uy+HjG5PpEhmi8vZgm0W7KDQ="
 })
@POST("login/facebook")
Call<LoginResponse> postFacebookData(@Body FacebookRequest request);

This does not:

@Headers({
"Content-Type: application/json"
})
@POST("login/facebook")
Call<LoginResponse> postFacebookData(
@Header("app-key") String hashKey,
@Body FacebookRequest request
);

Retrofit retrofit = RetrofitAdapter.getAdapter();
FacebookLoginAPI loginAPI = retrofit.create(FacebookLoginAPI.class);
-> loginAPI.postFacebookData("I99Uy+HjG5PpEhmi8vZgm0W7KDQ=", facebookRequest).enqueue(new Callback<LoginResponse>() {

How do I fix it?

Thanks

Most helpful comment

@prashantwosti Another way could be:
Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
this avoids wrapping with a platform specific newline character(s).

All 6 comments

0x0a is a newline character which is forbidden in a header (by OkHttp, not Retrofit). Ensure these control characters are stripped before passing them to Retrofit.

Thanks. So that means "=" is causing all the trouble here?

No, there's an invisible newline character (\n) after the "=". The "=" is character 27.

Thanks. .trim() did the job. :)

@prashantwosti Another way could be:
Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
this avoids wrapping with a platform specific newline character(s).

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

Was this page helpful?
0 / 5 - 0 ratings