Skip to content

Possibility to use addUnsafeNonAscii header on Request.Builder #7275

@iruizmar

Description

@iruizmar

When trying to add new headers inside an interceptor, I usually do the following:

OkHttpClient.Builder()
    .addInterceptor { chain ->
        chain.proceed(
             chain.request()
                 .newBuilder()
                 .addHeader(X, Y)
                 .build()
        )
    }

But what if I want to use the internal header builder addUnsafeNonAscii method? There isn't any shortcut for that.
The workaround is something like:

OkHttpClient.Builder()
    .addInterceptor { chain ->
        chain.proceed(
             chain.request()
                 .newBuilder()
                 //We need to use headers already in the request.
                 .headers(chain.request().headers.newBuilder().apply {
                     addUnsafeNonAscii(X, Y)
                 }.build())
                 .build()
        )
    }

It would be easy to add a new addUnsafeNonAscii method to Request.Builder that will use the internal header builder's addUnsafeNonAscii.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions