okhttp: IllegalArgumentException: Unexpected char 0xea

Viewed 1426

I am sending Latin character as HTTP header from my android code using okhttp

Builder builder=new Builder();
builder.url(myURL);
builder.addHeader("Original-Filename", "tête-à-tête.pdf");

But, I am getting IllegalArgumentException: Unexpected char 0xea at 1

I am able to send the same header from Postman or by using HttpsURLConnection. Also, the character is an excepted header format as per HTTP1.1 documentation.

So, then why is okHttp not supporting the following character? Also, is there any workaround this without changing code on the server-side by doing Base64 encoding and decoding.

I am using okttp version 4.4.0. Also, I have already gone through https://github.com/square/okhttp/issues/2016 before anyone repeats the same thing.

1 Answers

For anyone looking for the answer, I found the answer.

We can use addUnsafeNonAscii to solve this issue.

Below is the simple code snippet of it

Headers headers = new Headers.Builder()
.addUnsafeNonAscii("Original-Filename", "tête-à-tête.pdf")
.build();

By doing this we can avoid server-side changes, although in the long term it is suggested to use Base64 encoding on client-side and decoding that on the server-side.

Related