# Can't access headers from an HttpResponse \[ktor\]

**URL:** <https://discuss.kotlinlang.org/t/cant-access-headers-from-an-httpresponse-ktor/29657>\
**Category:** Support\
**Created:** [December 22, 2024, 4:45pm UTC](https://discuss.kotlinlang.org/t/cant-access-headers-from-an-httpresponse-ktor/29657 "2024-12-22T16:45:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bartlomiej](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bartlomiej](https://discuss.kotlinlang.org/u/Bartlomiej)\
**Post date:** [December 22, 2024, 4:45pm UTC](https://discuss.kotlinlang.org/t/cant-access-headers-from-an-httpresponse-ktor/29657/1 "2024-12-22T16:45:42Z")

</div>

Hi, I’ve made a simple rest api & website with a logjn/register system in kotlin that mostly works, but I’m having a problem with one thing. After successful registration I want to return an access token and a refresh token to the client through headers and it seems that the client gets these in the headers as I can see them in the browser console, but I can’t access them in code, as there seems to be no headers? That’s a bit weird and I don’t know what to do with it.

 ![New Project](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/b/b239f636fb7696176ec581ef34c5597f8657e863.png)

As you can see here, the headers are there, but in the console they’re not being printed. Even when using a javascript command to print the same thing, there are no headers. Why is that?

 ![New Project(1)](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/d/de596032e86c28946bca9c93842088ff4e533bc8.png)

Here’s the client code:

```kotlin
    button {
        +"Register"
        onClick = {
            if (!awaitingResponse) {
                scope.launch {
                    awaitingResponse = true
                    val result: HttpResponse = register(providedUsername, providedPassword)

                    // Log all headers first to see what's available
                    result.headers.forEach { name, value ->
                        console.log("Header: $name = $value")
                    }

                    console.log(result.headers)

                    val authToken = result.headers["auth-token"]
                    val refreshToken = result.headers["refresh-token"]

                    console.log("Auth-Token: $authToken")
                    console.log("Refresh-Token: $refreshToken")

                    if (authToken != null && refreshToken != null) {
                        saveTokens(authToken, refreshToken)
                    }

                    awaitingResponse = false
                }
            }
        }
    }

```

```auto
val jsonClient = HttpClient {
    install(ContentNegotiation) {
        json()
    }
}

suspend fun register(username: String, password: String): HttpResponse {
    return jsonClient.post("http://localhost:8080/api/user/register") {
        contentType(ContentType.Application.Json)
        setBody(UserRequest(username, password))
    }
}

```

and here’s the server code:

```auto
    post("/register") {

        val userRequest = call.receive<UserRequest>()

        val createdUser = userService.save(user = userRequest.toModel()) ?: return@post call.respond(HttpStatusCode.BadRequest)

        call.response.header(name = "id", value = createdUser.id.toString())

        call.response.header(name = "auth-token", value = jwtService.createAccessToken(createdUser.username))
        call.response.header(name = "refresh-token", value = jwtService.createRefreshToken(createdUser.username))

        logger.info(call.response.headers["auth-token"])
        logger.info(call.response.headers["refresh-token"])

        call.respond(message = HttpStatusCode.Created)

    }

```
