question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ktor sets default User-Agent even though custom one is defined in Default Headers

See original GitHub issue

Ktor Version and Engine Used (client or server and name) ktor-client-apache:1.3.1

Describe the bug Default “Ktor client” User-Agent is set even if User-Agent header value was previously configured using .setDefaultHeaders() for client.

To Reproduce Steps to reproduce the behavior:

  1. Create client instance and setDefaultHeader User-Agent:
val client = HttpClient(Apache) {
    engine {
        customizeClient {
            setDefaultHeaders(mapOf(
                "User-Agent" to "Test Agent"
            ).map { BasicHeader(it.key, it.value) })
        }
    }
}
  1. Perform any request:
client.request<String>("https://google.com/") {
            method = HttpMethod.Get
}

Expected behavior Request is done using User-Agent that was configured for the client using default headers and not default User-Agent for Ktor

Result Request is done using default Ktor UA header: Ktor client and not Test Agent that was sent when initializing client

Code issues The problem is in ktor-client-core-jvm-1.3.1-sources.jar!/commonMain/io/ktor/client/engine/Utils.kt class. It checks if there’s UA header in request itself or in content headers but never checks client default headers. Here are the corresponding lines of code:

    val missingAgent = requestHeaders[HttpHeaders.UserAgent] == null && content.headers[HttpHeaders.UserAgent] == null
    if (missingAgent && needUserAgent()) {
        block(HttpHeaders.UserAgent, KTOR_DEFAULT_USER_AGENT)
    }

Workaround Set User-Agent header for every request instead of setting it 1 time for the client

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dmitrievanthonycommented, Apr 27, 2020

Hi @dtolstyi, thanks for reporting! I’m working on it, but meanwhile, as a workaround, you could use UserAgent feature to set the user agent:

install(UserAgent) {
    agent = "Test Agent"
}
0reactions
dtolstyicommented, Apr 27, 2020

I see. Thank you for your answer @dmitrievanthony. Should we use defaultRequest block to specify default headers using ktor infrastructure?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default headers - Ktor
The DefaultHeaders plugin adds the standard Server and Date headers into each response. Moreover, you can provide additional default headers ...
Read more >
Making requests | Ktor
In this section, we'll see how to specify various request parameters, including an HTTP method, headers, and cookies. If you need to configure ......
Read more >
User agent | Ktor
The UserAgent plugin adds a User-Agent header to all requests. ... UserAgent only requires the ktor-client-core artifact and doesn't need ...
Read more >
io.ktor.http
Represents a header value that consist of content followed by parameters. Useful for headers such as Content-Type , Content-Disposition and so on.
Read more >
hostWithPort - ktor
Host:port pair, not normalized so port is always specified even if the port is schema's default. © 2022 CopyrightGenerated by dokka.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found