I get an error when trying to post to some website...
# ktor
j
I get an error when trying to post to some website, but I don't really understand that error because it doesn't really give me much information. Error: (see thread)
Exception in thread "DefaultDispatcher-worker-5" java.net.BindException: Cannot assign requested address: connect
at <http://sun.nio.ch.Net|sun.nio.ch.Net>.connect0(Native Method)
at <http://sun.nio.ch.Net|sun.nio.ch.Net>.connect(Net.java:482)
at <http://sun.nio.ch.Net|sun.nio.ch.Net>.connect(Net.java:474)
at <http://sun.nio.ch|sun.nio.ch>.SocketChannelImpl.connect(SocketChannelImpl.java:647)
at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:32)
at io.ktor.network.sockets.ConnectUtilsJvmKt.connect(ConnectUtilsJvm.kt:19)
at io.ktor.network.sockets.TcpSocketBuilder.connect(TcpSocketBuilder.kt:38)
at io.ktor.client.engine.cio.ConnectionFactory.connect(ConnectionFactory.kt:24)
at io.ktor.client.engine.cio.Endpoint$connect$2$connect$1.invokeSuspend(Endpoint.kt:184)
at io.ktor.client.engine.cio.Endpoint$connect$2$connect$1.invoke(Endpoint.kt)
at io.ktor.client.engine.cio.Endpoint$connect$2$connect$1.invoke(Endpoint.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturnIgnoreTimeout(Undispatched.kt:100)
at kotlinx.coroutines.TimeoutKt.setupTimeout(Timeout.kt:148)
at kotlinx.coroutines.TimeoutKt.withTimeoutOrNull(Timeout.kt:104)
at io.ktor.client.engine.cio.Endpoint.connect(Endpoint.kt:192)
at io.ktor.client.engine.cio.Endpoint.access$connect(Endpoint.kt:28)
at io.ktor.client.engine.cio.Endpoint$makeDedicatedRequest$1.invokeSuspend(Endpoint.kt:105)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
When does this error occur?
Any ideas?
I just tried to use the apache engine instead of jetty and cio, and now I get a different error Btw code:
Copy code
val result = <http://http.post|http.post><String>("<https://discord.com/api/v9/users/@me/channels>", body = buildJsonObject {
    put("recipient_id", 285418000734093312)
}.toString()) {
    header("Authorization", "")
    header("User-Agent", "")
}
println(result)
New error:
Exception in thread "main" io.ktor.client.features.ClientRequestException: Client request(<https://discord.com/api/v9/users/@me/channels://localhost/>) invalid: 404 Not Found. Text: "{"message": "404: Not Found", "code": 0}"
Why is there a localhost now??
a
The problem is that first parameter of the
<http://HttpClient.post|HttpClient.post>
overload you use is scheme so it adds
://localhost
to it. Here is a correct way of making a POST request with a body:
Copy code
val client = HttpClient(Apache)

val result = <http://client.post|client.post><String>("<https://discord.com/api/v9/users/@me/channels>") {
    body = buildJsonObject {
        put("recipient_id", 285418000734093312)
    }.toString()

    header("Authorization", "")
    header("User-Agent", "")
}
println(result)
j
Oh thanks that fixed everything! 👍