Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dylech30th opened this issue Jul 8, 2020 · 4 comments
Assignees

Comments

@dylech30th
Copy link

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:

suspend fun requestJson(hostname: String): DnsResponse {
            HttpClient(CIO) {
                install(JsonFeature) {
                    serializer = KotlinxSerializer()
                }
                install(HttpTimeout)
            }.use {
                return withContext(Dispatchers.IO) {
                    it.get<DnsResponse>("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

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

{"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

@dylech30th dylech30th added the bug label Jul 8, 2020
@dylech30th dylech30th changed the title URLConnection works fine but ktor clinet returns 403 forbidden during a get request URLConnection works fine but ktor client returns 403 forbidden during a get request Jul 8, 2020
@e5l e5l self-assigned this Jul 13, 2020
@e5l e5l added the triaged label Jul 13, 2020
@oleg-larshin
Copy link

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

@Stexxe
Copy link
Contributor

Stexxe commented Mar 23, 2021

Most likely this server supports only HTTP/2 protocol but the CIO engine supports the only HTTP/1.1. As a workaround use the Jetty engine.

@vkarthikeyan
Copy link

Facing the same issue
Fatal Exception: io.ktor.client.features.ClientRequestException: Client request(URL) 403 invalid
and using ktor v1.6.3.
Is this issue fixed? If so, in which version?

@Stexxe
Copy link
Contributor

Stexxe commented Mar 13, 2024

This exception is thrown by the response validation. In Ktor 1.*, the response validation is enabled by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants