android - 如何删除 Ktor 客户端 2.0.0 的默认 header

标签 android kotlin ktor kotlin-multiplatform-mobile ktor-client

适用于 Android 的 Ktor v2.0.0。
默认的 ktor header “Accept”只是覆盖了我的“Accept” header 。
这是我初始化 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
}
但是当我检查查尔斯拦截器时,我的标题看起来像:
enter image description here
我有这个

application/json


这会覆盖我的 application/vnd...值(value)。
有没有办法替换默认的“接受”标题?

最佳答案

您可以删除 Accept来自 defaultRequest 的标题阻止并注册 GsonConverter 的内容类型而不是使用默认的 application/json内容类型。

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

关于android - 如何删除 Ktor 客户端 2.0.0 的默认 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72437713/

相关文章:

android - Android 中的单元测试?

android - HAX 正在工作并且模拟器在快速 virt 模式下运行

java - Kotlin 委托(delegate)给 future

kotlin - 如何禁用 Ktor 客户端 SSL 验证?

kotlin - 如何在 Kotlin 协程中使用阻塞(I/O 绑定(bind))API?

java - 从网页解析 JSON 文件并将值存储在二维数组中

android - 在SD卡中找不到隐藏的文件

kotlin - 如何在 Kotlin 中将数字转换为字符?

regex - Kotlin 正则表达式边界匹配不工作

kotlin - 数据类中的默认参数未使用 ktor 序列化程序转换为 json