James Black
04/15/2022, 5:39 AMimplementation("org.jetbrains.kotlin:kotlin-stdlib")
And
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10")
I am using ktor 2.0.0 and I also have this in build.gradle.kts:
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
zeugederunity
04/15/2022, 6:24 AMkotlin {
val ktorVersion: String by project /* 2.0.0 */
jvm { /* some blabla */ }
js { /* some blabla */ }
sourceSet {
val commonMain by getting { /* some blabla */ }
val jsMain by getting { /* some blabla */ }
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.ktor:ktor-server-status-pages:$ktorVersion")
/*And some more */
}
}
val jvmTest by getting {
dependencies {
implementation("io.ktor:ktor-server-test-host:$ktorVersion")
}
}
}
}
The external libraries look like this:
/*ktor-server-auth has no jvmAndNixMain*/
Gradle: io.ktor:ktor-server-auth-jvm:2.0.0
Gradle: io.ktor:ktor-server-auth-jwt-jvm:2.0.0
Gradle: io.ktor:ktor-server-auto-head-response:jvmAndNixMain:2.0.0
Gradle: io.ktor:ktor-server-auto-head-respone:2.0.0
/*Some more libs with the each the corresponding jvmAndNixMain */
Gradle: io.ktor:ktor-server-core-jvm:jvmAndNixMain:2.0.0
Gradle: io.ktor:ktor-server-core-jvm:2.0.0
Is this a bug or does anyone know what I did wrong?
Thank you for your help in advance!ribesg
04/15/2022, 2:45 PM"new"
parameter value make any sense on this specific line? https://github.com/ktorio/ktor-documentation/blob/main/codeSnippets/snippets/resource-routing/src/main/kotlin/com/example/Application.kt#L19Viktor Orlyk
04/15/2022, 6:20 PMS.
04/15/2022, 6:37 PMget().body<List<Class>>()
apparently also expects the json input to be an array and when it only returns one object I always run into kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead
is there a way around this other than catching the exception and .body<Class>
?Viktor Orlyk
04/17/2022, 8:10 AMval client = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
}
my ide shows an error in there
'fun <P : Pipeline<*, ApplicationCall>, B : Any, F : Any> Application.install(plugin: Plugin<Application, ContentNegotiationConfig, PluginInstance>, configure: ContentNegotiationConfig.() -> Unit = ...): PluginInstance' can't be called in this context by implicit receiver. Use the explicit one if necessary
David Stibbe
04/17/2022, 3:08 PMAuthentication.Feature.AuthenticatePhase
phase. However, this phase does not exist anymore. According to the documentation there is a CheckAuthentication
phase. Is there a specific constant that identifies this phase so I can register our plugin after this specific phase?Nabil
04/17/2022, 5:49 PM2.0.0
compatible with coroutines 1.6.1-native-mt
using the old memory model? I'm under the impression only the new memory model is supported after this was merged https://github.com/ktorio/ktor/pull/2739 ?
Using the old memory model throws
kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
at kotlin.Throwable#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:24)
at kotlin.Exception#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:23)
at kotlin.RuntimeException#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:34)
at kotlin.native.IncorrectDereferenceException#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt:34)
at <global>.ThrowIncorrectDereferenceException(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt:105)
at <global>.CheckGlobalsAccessible(Unknown Source)
at io.ktor.client.plugins#<get-PLUGIN_INSTALLED_LIST>(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/HttpClientPlugin.kt:11)
at io.ktor.client.HttpClientConfig.install$<anonymous>_1-3#internal(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:74)
at io.ktor.client.HttpClientConfig.$install$<anonymous>_1-3$FUNCTION_REFERENCE$7.invoke#internal(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:122)
at io.ktor.client.HttpClientConfig.$install$<anonymous>_1-3$FUNCTION_REFERENCE$7.$<bridge-UNNN>invoke(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:122)
at io.ktor.client.HttpClientConfig#install(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:96)
at io.ktor.client.HttpClient#<init>(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:165)
at io.ktor.client.HttpClient#<init>(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:82)
at io.ktor.client#HttpClient(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:42)
at io.ktor.client#HttpClient(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/posix/src/io/ktor/client/HttpClient.kt:20)
at io.ktor.client#HttpClient$default(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/posix/src/io/ktor/client/HttpClient.kt:18)
James Black
04/18/2022, 4:26 AMcurl -X POST -d 'Hello' <http://0.0.0.0:8080/outfit_data>
success%
The error I get from android is:
I/System.out: Failed to connect to /0.0.0.0:8080 calling <http://0.0.0.0:8080/outfit_data>
This is my ktor 2.0.0 routing:
post("/outfit_data") {
val data = call.receive<String>()
println(data)
call.respondText("success", null, HttpStatusCode.OK)
}
And on the android side this is how I make the call, and baseUrl is set as this is the top-level gradle.properties file
weather_client_host_name=<http://0.0.0.0:8080>
try {
<http://client.post|client.post>(
"$baseUrl/outfit_data"
) {
contentType(ContentType.Application.Json)
setBody(data)
}.also { response ->
return response.status == HttpStatusCode.OK || response.status == HttpStatusCode.Created
}
} catch(e:Throwable) {
println("${e.message} calling $baseUrl/outfit_data")
return false
}
My code is in the androidApp, shared/androidMain and server subprojects. The http call for Android is in shared/src/androidMain/.../api/WeatherApi.kt
I also put this in the android manifest in the shared/src/androidMain:
<application
android:usesCleartextTraffic="true">
https://github.com/jblack975/MyOutfitPicker/tree/pass_anon_data_to_serversuresh
04/18/2022, 10:55 PMVivek Modi
04/19/2022, 11:53 AMBearer
keyword from authorization
token in Ktor. I don't want to use these keyword in my api call. I think it call authorization header, but I am not sure. Thanks
install(Auth) {
bearer {
loadTokens {
BearerTokens("u_b6I4kItp", "xyz111")
}
}
}
Vivek Modi
04/19/2022, 3:08 PMHttpResponseValidator
is used for api status, correct me If I am wrong.
1. I need to call api, if api returns 401 status, I need to call refershToken api. After getting new accessToken from refreshToken api, I need to send this to api call.
2. If refreshToken is giving 401 then I need to infrom my application to logout.
How can I achieve through this Ktor?Alex
04/20/2022, 11:36 AMVivek Modi
04/20/2022, 2:45 PM@Body
in retrofit. what is alternative in ktor?Mark Malik
04/20/2022, 3:18 PMLastExceed
04/21/2022, 11:39 AMJason Han
04/21/2022, 8:08 PMRenaud
04/22/2022, 1:58 PMNo request transformation found
. Does it sound familiar to someone?
Looking a ktor source code, it comes from HttpRequest.kt
body as? OutgoingContent ?: error("No request transformation found: $body")
It seems related to the setBody function in my test
@Test
fun testPostRandom() = testApplication {
val response = <http://client.post|client.post>("/random") {
contentType(ContentType.Application.Json)
setBody(PostRandomRequest(listOf("Jet", "Brains")))
}
assertEquals("Hello World!", response.bodyAsText())
assertEquals(HttpStatusCode.Created, response.status)
}
Nikky
04/23/2022, 12:29 AMoptional=true
? in my code when the token verification fails it always responds with 401, even when set up like so
authenticate("auth-jwt", optional = true) {
any tricks that i can do to evad whats in JWTUtils.kt#verifyAndValidate at lines 96-99
?
from how i read the documentation a failed auth should lead to a principal being null.. but in this case it never hits my code and always just jumps to the challenge...
PS: diging into the code.. seems like AuthenticationFailedCause.InvalidCredentials
is still responded with a 401
and token verification failure, eg. expired token or signing key differs or such.. will trigger that
i'd prefer to treat it as if it was a AuthenticationFailedCause.NoCredentials
for now i copied the JWTAuthenticationProvider
and all the internal classes it uses.. just to change one line...russhwolf
04/23/2022, 3:36 AM@Test
fun errorTest() = runTest {
val engine = MockEngine {
respondError(HttpStatusCode.NotFound)
}
val httpClient = HttpClient(engine) {
install(ContentNegotiation) {
json()
}
}
assertFailsWith<ClientRequestException> { httpClient.get("<https://example.com/>").body<Message>() }
}
@Serializable
data class Message(val data: String)
In 2.0.0 the test fails with Expected an exception of class io.ktor.client.plugins.ClientRequestException to be thrown, but was io.ktor.client.call.NoTransformationFoundException: No transformation found: class io.ktor.utils.io.ByteBufferChannel
. It seems like the http client is attempting to deserialize the error response rather than throwing due to the unsuccessful status code. Was there a deliberate behavior change here or is this a bug?brabo-hi
04/23/2022, 9:58 PMktor-2.0
handleResponseException has been deprecated in replacement of handleResponseExceptionWithRequest
. However i am having exception when accessing request.call.response.status
Viktor Orlyk
04/24/2022, 12:00 PMHayden Meloche
04/25/2022, 12:24 AM*-jvm
libraries and I believe I need to generate a file like thisMuhammad Talha
04/25/2022, 7:46 AMUnresolved reference: ContentNegotiation
error in com/talhaguy/plugins/Serialization.kt
. Does any one know how I can get past this error? Thanks in advance!
I have a feeling not everything I needed got generated. I was following the https://ktor.io/docs/creating-http-apis.html#source_code tutorial and the /src/main/resources/application.conf
did not get generated either and I manually created it.Louis
04/25/2022, 11:20 AMJari Ruokonen
04/25/2022, 2:52 PM@Test
fun `400 Bad request on item sync when request body cannot be parsed`() {
val invalidItem = "invalid body"
withTestApplication({
restService(connector, synchronizer, logger)
}) {
handleRequest(true) {
method = <http://HttpMethod.Post|HttpMethod.Post>
uri = "/synchronize/item"
setBody(invalidItem)
}.apply {
assertEquals(HttpStatusCode.BadRequest, response.status())
}
}
}
This particular test fails with following status:
expected: <400 Bad Request> but was: <404 Not Found>
Expected : 400 Bad Request
Actual : 404 Not Found
If I place breakpoints to the tested restService code, they are never reached, like the 404 error hints. I also placed breakpoint to start() method of the RestService and it's being called at the start of the test as expected and I can step through it without errors.
If I revert Ktor back to 1.4.2, all the tests run ok - so, what exactly could have changed from 1.4.2 to 1.6.8 that breaks these? The change log is quite lengthy, so I ask for an educated advise.Martin Gaens
04/25/2022, 3:16 PMth:utext
? I'm trying to render a page using Thymeleaf but there are certain parts of the website which are much easier rendered using kotlinx.html
. I'd like to inject them using th:utext
but this Stackoverflow answer and a comment underneath it says it's a security vulnerability.MBegemot
04/26/2022, 9:49 AMRescribet
04/26/2022, 10:33 AMio.ktor.util.*
, but I'm seeing this mysterious asterisk after the package in the gradle menu.
Adding/removing a dependency (io.ktor:ktor-utils:2.0.0
) to commonMain
and/or jvmMain
doesn't fix the missing package (can't click through to io.ktor.util.Attributes
from ApplicationCall
)Satyam Agarwal
04/27/2022, 9:27 AMjava.io.EOFException: Invalid chunk: content block of size 8192 ended unexpectedly
at io.ktor.http.cio.ChunkedTransferEncodingKt.decodeChunked(ChunkedTransferEncoding.kt:92)
at io.ktor.http.cio.ChunkedTransferEncodingKt$decodeChunked$3.invokeSuspend(ChunkedTransferEncoding.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.internal.DispatchedContinuation.resumeWith(DispatchedContinuation.kt:205)
at io.ktor.utils.io.ByteBufferChannel.resumeWriteOp(ByteBufferChannel.kt:2191)
at io.ktor.utils.io.ByteBufferChannel.bytesRead(ByteBufferChannel.kt:908)
at io.ktor.utils.io.ByteBufferChannel.readAsMuchAsPossible(ByteBufferChannel.kt:533)
at io.ktor.utils.io.ByteBufferChannel.readAsMuchAsPossible$default(ByteBufferChannel.kt:514)
at io.ktor.utils.io.ByteBufferChannel.readRemainingSuspend(ByteBufferChannel.kt:2154)
at io.ktor.utils.io.ByteBufferChannel.readRemaining$suspendImpl(ByteBufferChannel.kt:2128)
at io.ktor.utils.io.ByteBufferChannel.readRemaining(ByteBufferChannel.kt)
at io.ktor.utils.io.ByteReadChannelKt.readRemaining(ByteReadChannel.kt:212)
at io.ktor.util.ByteChannelsKt$split$1.invokeSuspend(ByteChannels.kt:25)
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)
Satyam Agarwal
04/27/2022, 9:27 AMjava.io.EOFException: Invalid chunk: content block of size 8192 ended unexpectedly
at io.ktor.http.cio.ChunkedTransferEncodingKt.decodeChunked(ChunkedTransferEncoding.kt:92)
at io.ktor.http.cio.ChunkedTransferEncodingKt$decodeChunked$3.invokeSuspend(ChunkedTransferEncoding.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.internal.DispatchedContinuation.resumeWith(DispatchedContinuation.kt:205)
at io.ktor.utils.io.ByteBufferChannel.resumeWriteOp(ByteBufferChannel.kt:2191)
at io.ktor.utils.io.ByteBufferChannel.bytesRead(ByteBufferChannel.kt:908)
at io.ktor.utils.io.ByteBufferChannel.readAsMuchAsPossible(ByteBufferChannel.kt:533)
at io.ktor.utils.io.ByteBufferChannel.readAsMuchAsPossible$default(ByteBufferChannel.kt:514)
at io.ktor.utils.io.ByteBufferChannel.readRemainingSuspend(ByteBufferChannel.kt:2154)
at io.ktor.utils.io.ByteBufferChannel.readRemaining$suspendImpl(ByteBufferChannel.kt:2128)
at io.ktor.utils.io.ByteBufferChannel.readRemaining(ByteBufferChannel.kt)
at io.ktor.utils.io.ByteReadChannelKt.readRemaining(ByteReadChannel.kt:212)
at io.ktor.util.ByteChannelsKt$split$1.invokeSuspend(ByteChannels.kt:25)
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)
Aleksei Tirman [JB]
04/27/2022, 4:59 PMSatyam Agarwal
04/27/2022, 5:06 PMAleksei Tirman [JB]
04/27/2022, 5:10 PMSatyam Agarwal
04/27/2022, 5:13 PM