ktorio / ktorio/ktor

URLConnection works fine but ktor client returns 403 forbidden during a get request

Open
#1,994 4 comments 0 reactions 1 assignee Claimed by @e5l View on GitHub
bug triaged
Dominant language
Kotlin
Stars
14.5k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
49

Description

I'm using ktor 1.3.2 with `CIO` engine, when I try to send a get request to URL `https://1.0.0.1/dns-query?ct=application/dns-json&cd=false&do=false&type=A&name=google.com` by the following code:
```kotlin
suspend fun requestJson(hostname: String): DnsResponse {
HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(HttpTimeout)
}.use {
return withContext(Dispatchers.IO) {
it.get("https://1.0.0.1/dns-query?ct=application/dns-json&cd=false&do=false&type=A&name=google.com") {
timeout {
requestTimeoutMillis = 5000
}
}
}
}
}
```
an error occurred with the message `Exception in thread "main" io.ktor.client.features.ClientRequestException: Client request(https://1.0.0.1/dns-query?ct=application%2Fdns-json&cd=false&do=false&type=A&name=google.com) invalid: 403 Forbidden`, but when I try to access this URL by using a browser or postman or even using the old-school URLConnection in java, it works fine and returns the correct response, for example, the code
```kotlin
val url = URL("https://1.0.0.1/dns-query?ct=application/dns-json&cd=false&do=false&type=A&name=google.com")
val conn = url.openConnection() as HttpURLConnection
conn.connect()
val bf = BufferedReader(InputStreamReader(conn.inputStream))
var line: String = ""
while (bf.readLine().let { if (it == null) false else {line += it; true} }) {

}
println(line)
```
will print
```json
{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":true,"Question":[{"name":"google.com","type":1}],"Answer":[{"name":"google.com","type":1,"TTL":59,"data":"172.217.5.78"}]}
```
and everything looks fine

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.