Hello,I started a new Android project.This time I ...
# ktor
m
Hello,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.
h
useDefaultTransformers = false
a
Can you share the code snippets with Retrofit and Ktor to determine the difference?
m
I configured ktor client like this. I used Retrofit as a test, very simply.
a
Can you share the endpoint you're trying to make a request to?
m
Copy code
"<https://lezzet-tarifleri.com/>"
In addition, I do not have this problem when I send a request with cURL or Postman. I only have this problem when I send a request with Ktor. When I use useDefaultTransformer = false, I can send a request, but I think this is not exactly the right method, we lose some functionality such as response.readText()
a
The problem is in the
Accept-Charset: UTF-8
header which is added if the
HttpPlainText
plugin is installed.
As a workaround, you can remove that header using the following code:
Copy code
val client = HttpClient(CIO) {
    install(createClientPlugin("fix") {
        on(Send) { request ->
            request.headers.remove("Accept-Charset")
            this.proceed(request)
        }
    })
}
m
This worked, thank you very much. I had previously tried to remove the header within the defaultRequest block and the HttpRequestBuilder block, but it just wouldn’t delete. Your method did the trick.