https://kotlinlang.org logo
#ktor
Title
# ktor
m

Marko Kunic

11/04/2021, 9:32 AM
I am trying to append a header to my request, documentation says it is a string, but when I pass a string I get a type mismatch:
Copy code
Type mismatch.
Required: HeaderValueWithParameters
Found: String
HeaderValueWithParameters is abstract, not sure what to pass here?
a

Aleksei Tirman [JB]

11/04/2021, 9:37 AM
Could you please share your code for appending a header to a request?
An example:
Copy code
<http://client.post|client.post><String>("<https://example.com>") {
    headers {
        append("name", "value")
    }
}
m

Marko Kunic

11/04/2021, 9:40 AM
Copy code
client.request(uri.toString()) {
  headers {
    @OptIn(InternalAPI::class)
    append("test", "test")
  }
  HttpMethod(mymethod.toString())
  body = mybody
}
1.6.5 version if it helps
so it is calling
append
from
io.ktor.http
and not
io.ktor.util
, docs mention later, while my code is using former
a

Aleksei Tirman [JB]

11/04/2021, 11:19 AM
The following code works as expected (
append
is imported from
io.ktor.http
package):
Copy code
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.util.InternalAPI
import kotlinx.coroutines.coroutineScope

suspend fun main(): Unit = coroutineScope {
    val client = HttpClient(Apache) {}

    client.request("<https://httpbin.org/post>") {
        headers {
            @OptIn(InternalAPI::class)
            append("test", "test")
        }
        method = <http://HttpMethod.Post|HttpMethod.Post>
        body = "body"
    }
}
m

Marko Kunic

11/04/2021, 11:20 AM
oh, is it maybe because I am using Java engine and not apache?
nop, same problem
just tested your example, and I get the same error
@Aleksei Tirman [JB] what versions are you using? because I am trying with: Java 16 Kotlin 1.5.31 ktor 1.6.5 and I am still getting type mismatch
Copy code
import com.mycompany.sdk.other.API.HttpMethod
import com.mycompany.sdk.other.API.HttpClient
import com.mycompany.sdk.other.API.Response
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.InternalAPI;
import kotlinx.coroutines.runBlocking
import java.net.URI
import io.ktor.client.HttpClient as InnerKtorHttpClient
import io.ktor.http.HttpMethod as KtorHttpMethod

class KtorHttpClient(private val client: InnerKtorHttpClient) : HttpClient {
    override fun request(uri: URI, method: HttpMethod, body: String, headers: Map<String, String>): Response {
        val response: HttpResponse = runBlocking {
            client.request(uri.toString()) {
                headers {
                    @OptIn(InternalAPI::class)
                    headers.forEach { header -> append(header.key, header.value) }
                }
                KtorHttpMethod(method.toString())
                this.body = body
            }
        }

        return Response(response.status.value, response.toString())
    }
}
this is my real code if it makes a difference?
a

Aleksei Tirman [JB]

11/05/2021, 9:48 AM
Your code compiles just fine on my machine with the same environment.
m

Marko Kunic

11/05/2021, 10:05 AM
Thanks for help, gave up in the end and went with native java client
a

Aleksei Tirman [JB]

11/05/2021, 12:47 PM
You can try cleaning the Gradle cache (delete ~/.m2 and ~/.gradle folders) to fix your problem.
12 Views