Home > OS >  How to remove default header for Ktor client 2.0.0
How to remove default header for Ktor client 2.0.0

Time:05-31

Ktor v2.0.0 for Android.

The default ktor header "Accept" just overwrites my "Accept" header.

This is the way I am initializing my HttpClient:

  HttpClient(Android) {
    defaultRequest {
        header("Key", BuildConfigCore.API_KEY)
        header("Accept", "application/vnd.***.mobile-v8 json")
      
        host = hostAddress
        url.protocol = URLProtocol.HTTPS
    }
    install(Logging) {
        if (BuildConfig.DEBUG) {
            logger = Logger.DEFAULT
            level = LogLevel.ALL
        }
    }
    install(ContentNegotiation) {
        gson()
    }
    expectSuccess = false
}

But when i check with Charles interceptor, my headers looks like: enter image description here

I have that

application/json

which overwrites my application/vnd... value.

Is it any way to replace the default "Accept" header?

CodePudding user response:

You can remove the Accept header from the defaultRequest block and register your content type for the GsonConverter instead of using the default application/json content type.

val client = HttpClient {
    install(ContentNegotiation) {
        gson(ContentType("application", "vnd.***.mobile-v8 json"))
    }
}
  • Related