Hi Everyone, I'm new with ktor and I'm trying to r...
# ktor
d
Hi Everyone, I'm new with ktor and I'm trying to run this small example and I see that my request headers are not being set 😞
Copy code
suspend fun main() {

    val token = "foobar"
    val httpClient = HttpClient(CIO)
    val response: HttpResponse =
        httpClient.get("<https://echo.free.beeceptor.com/>") {
            headers {
                append(HttpHeaders.Authorization, "Bearer $token")
                append(HttpHeaders.Accept, ContentType.Application.Json.toString())
            }
        }

    println(response.bodyAsText())

}
And the response I'm getting back is
Copy code
{
  "method": "GET",
  "protocol": "https",
  "host": "<http://echo.free.beeceptor.com|echo.free.beeceptor.com>",
  "path": "/",
  "ip": "<http://xxx.xxx.xxx.xxx:4167|xxx.xxx.xxx.xxx:4167>",
  "headers": {
    "Host": "<http://echo.free.beeceptor.com|echo.free.beeceptor.com>",
    "User-Agent": "Ktor client",
    "Accept": "*/*",
    "Accept-Charset": "UTF-8",
    "Accept-Encoding": "gzip"
  },
  "parsedQueryParams": {}
}
h
Copy code
val token = "foobar"
    val httpClient = HttpClient(CIO)
Copy code
val response: HttpResponse = httpClient.get("<https://echo.free.beeceptor.com/|https://echo.free.beeceptor.com/>") {
        headers {
            append(HttpHeaders.Authorization, "Bearer $token")
            append(HttpHeaders.Accept, ContentType.Application.Json.toString())
        }
    }
Copy code
println("Response status: ${response.status}")
    println("Response headers: ${response.headers}")
    println("Response body: ${response.bodyAsText()}")
httpClient.close()
Try printing the headers and see
a
It's a guess, but I think the headers function is not setting headers, but the just creating object Try headers.append
h
Also the server might be stripping or not echoing certain headers for security reasons, try local server and see.
d
I tried first with local server and route it is still the same, I tried to print them out and there are no headers at all. Also I tested with curl this server and it is echoing back everything.
a
I see both headers in the response when I execute your code. Can you please share the imports? What version of Ktor do you use?
a
I’m somehow struggling with the same that headers are not being set correctly. Just for my understanding should the following code snippets bring the same outcome? Im on 2.3.12. First snippet is from the ktor docs
Copy code
client.get("<https://ktor.io>") {
    headers {
        append(HttpHeaders.Accept, "text/html")
        append(HttpHeaders.Authorization, "abc123")
        append(HttpHeaders.UserAgent, "ktor client")
    }
}
and
Copy code
client.get("<https://ktor.io>") {
    headers.append(HttpHeaders.Accept, "text/html")
    headers.append(HttpHeaders.Authorization, "abc123")
    headers.append(HttpHeaders.UserAgent, "ktor client")
}
when i run these i get the following outputs from logging:
Copy code
0:55:52.063 [DefaultDispatcher-worker-9 @coroutine#42] INFO  io.ktor.client.HttpClient - REQUEST: <https://ktor.io>
METHOD: HttpMethod(value=GET)
COMMON HEADERS
-> Accept: application/json
-> Accept-Charset: UTF-8
CONTENT HEADERS
-> Content-Length: 0
BODY Content-Type: null
BODY START

BODY END
10:55:52.435 [DefaultDispatcher-worker-4 @coroutine#193] INFO  io.ktor.client.HttpClient - REQUEST: <https://ktor.io>
METHOD: HttpMethod(value=GET)
COMMON HEADERS
-> Accept: text/html; application/json
-> Accept-Charset: UTF-8
-> Authorization: abc123
-> User-Agent: ktor client
CONTENT HEADERS
-> Content-Length: 0
BODY Content-Type: null
BODY START
Hmm i guess i found my issue. There is
Copy code
io.ktor.http.HttpMessageBuilder.headers
and
Copy code
io.ktor.http.headers
One adds your headers and one creates a Headers object … and you if don’t pay attention to the imports you get this behavior … 😅