Ktor getting 403 forbidden error when requesting api using cloudflare

160 Views Asked by At

I started a new Android project.This time I decided to use Ktor instead of Retrofit as a network client.The api uses CloudFlare.When I send a request to the api, I get a 403 Forbidden Error.If I use Retrofit, this problem does not occur.I think the problem is in the common headers that Ktor sends, but I cannot delete the headers no matter what I do.How can I solve this problem? I don’t want to use Retrofit.

1

There are 1 best solutions below

1
On BEST ANSWER

The problem is in the Accept-Charset: UTF-8 header which is added by Ktor by default. As a workaround, you can remove that header using a plugin:

val client = HttpClient(CIO) {
    install(createClientPlugin("fix") {
        on(Send) { request ->
            request.headers.remove("Accept-Charset")
            this.proceed(request)
        }
    })
}