Phil Richardson
04/10/2022, 11:40 AMct
04/10/2022, 3:57 PM@Serializable
data class Agent(val id: Int, val hostname: String, val port: Int)
I’m struggling with handling POST:
@Serializable
@Resource("/api/agents")
data class AgentsRoute(val sort: String? = "id") {
@Serializable
@Resource("{id}")
data class Id(val parent: AgentsRoute = AgentsRoute(), val id: Int)
}
fun Application.agentRoutes() {
val agentService = AgentService() // Talks to the database
routing {
get<AgentsRoute> {
call.respond(agentService.getAll())
}
get<AgentsRoute.Id> { request ->
val agent = agentService.get(request.id) ?: call.respond(
HttpStatusCode.NotFound, "Agent not found"
)
call.respond(agent)
}
post<AgentsRoute> {
val agent = call.receive<Agent>() // This blows up!
// TODO
}
delete<AgentsRoute.Id> { request ->
agentService.get(request.id) ?: call.respond(
HttpStatusCode.NotFound, "Agent not found"
)
call.respond(agentService.delete(request.id))
}
}
}
I’m having a problem with the POST
when I post some data to create a new agent:
{
"hostname": "192.168.1.1",
"port": 1012
}
Here’s the exception:
2022-04-10 16:51:06.740 [eventLoopGroupProxy-4-1] ERROR Application - Unhandled: POST - /api/agents/
kotlinx.serialization.MissingFieldException: Field 'id' is required for type with serial name 'com.sonalake.snmpsim.model.Agent', but it was missing
at kotlinx.serialization.internal.PluginExceptionsKt.throwMissingFieldException(PluginExceptions.kt:20)
I understand why it’s happening - the JSON doesn’t contain an id property - but, as I want to create a new agent I don’t want to have to provide one.
I had a look at the Ktor 2.0.0 resource-routing code snippet, but it didn’t go further than receiving the request. Any suggests on the idiomatic Ktor 2 way of doing this?
Thank you 🙇Nick Halase
04/10/2022, 5:33 PMinstall(Routing)
and others just declare routing directly? Is the install for the routing module really optional?hfhbd
04/11/2022, 4:01 AM@KtorDsl val myPlugin = createRouteScopedPlugin("myPlugin, ::Config) {}
AND to @KtorDsl class Config
too?hhariri
04/11/2022, 1:35 PMVivek Modi
04/11/2022, 3:41 PMContentNegotiation
in ktor in client side? Can someone guide me.dodalovic
04/11/2022, 6:36 PMhawklike
04/11/2022, 7:35 PMByteZ
04/11/2022, 9:11 PMon
functions in plugins? Do I simply not have to call finish
and proceed
anymore? Will ktor automatically detect that the request has already received a response and not pass it further down the pipeline?Landry Norris
04/11/2022, 9:24 PMDomain specific configurations require that hostname aware checkServerTrusted(X509Certificate[], String, String) is used
The SO answers I found say this function should not be used in this way. Is there a way to configure the CIO engine to avoid this method?Jan
04/12/2022, 7:36 AMsindrenm
04/12/2022, 10:24 AMhandleResponseException
supposed to also catch `io.ktor.client.call.NoTransformationFoundException`s? Cause it just seems to fall through in my case. I can see it catching other non-HTTP exceptions such as `kotlinx.serialization.MissingFieldException`s, for instance, and of course `io.ktor.client.features.ClientRequestException`s and `io.ktor.client.features.ServerResponseException`s. I'm on Ktor v1.6.7 on Android using the CIO
engine.Júlio Santos
04/12/2022, 5:00 PMcom.github.nielsfalk:ktor-swagger:pom:v0.7.0
? I have this error Could not find artifact com.github.nielsfalk:ktor-swagger:pom:v0.7.0 in maven-central (<https://repo1.maven.org/maven2>)
!
I believe it may have changed repositories, I’ve been searching and haven’t found anything about a possible changePHaroZ
04/12/2022, 5:13 PMHttpTimeout
feature ; I try to figure out were I can catch the HttpRequestTimeoutException
thrown but I don't find any clean and generic solution.
Here is my config :
val engine = TODO("CIO.create() or MockEngine")
val ktorBaseClient = HttpClient(engine) {
install(HttpTimeout)
}
val ktorClient = ktorBaseClient.config {
HttpResponseValidator {
handleResponseException { t ->
when (t) {
// -----> next line works with MockEngine but not with CIO.create()
is HttpRequestTimeoutException -> throw MyCustomException()
else -> throw t
}
}
validateResponse { response ->
// it doesn't seem do by the right place to catch HttpRequestTimeoutException
...
}
}
}
ktorClient.get("some_uri") {
timeout { requestTimeoutMillis = 1.seconds.inWholeMilliseconds }
}
Note that to simulate a timeout via the MockEngine I do something like
MockEngine {
delay(2.seconds.inWholeMilliseconds)
throw IllegalStateException("should never be called")
}
and it works, it throws a MyCustomException
; but this is not the case in production code, with the CIO engine.
Does someone have any advice or example ? thx for your help.Jim
04/12/2022, 6:26 PMjava.lang.IllegalStateException: FATAL ERROR: Could not find "io.ktor:ktor-http-cio"
- has anyone seen anything like this lately? Searching the slack I found an error in #apollo-kotlin but didn't get anywhere with that threadVivek Modi
04/13/2022, 11:00 AMsindrenm
04/13/2022, 11:09 AMHttpRequestBuilder
when configuring DefaultRequest
, but I'm instead in the scope of a DefaultRequestBuilder
. AFAICT, they don't share the same structure. Previously, in ktor-client v1.6.7, I was able to access body
inside of this builder scope, but no more.
Docs:
val client = HttpClient(CIO) {
defaultRequest {
// this: HttpRequestBuilder
}
}
Actual (ktor-client-core-jvm-2.0.0.jar):
public class DefaultRequest private constructor(private val block: DefaultRequestBuilder.() -> Unit) {
public companion object Plugin : HttpClientPlugin<DefaultRequestBuilder, DefaultRequest> {
// ...
}
// ...
}
// Hence:
defaultRequest {
// this: io.ktor.client.plugins.DefaultRequest.DefaultRequestBuilder
}
Is this by design? Are the docs wrong here?Robert Jaros
04/13/2022, 1:25 PMCannot access class 'kotlinx.serialization.json.Json'. Check your module classpath for missing or conflicting dependencies
when I try to use content negotiation:
install(ContentNegotiation) {
json(json)
}
I checked the dependencies but I can't see any obvious conflicts.d.medina
04/13/2022, 1:30 PMrouting() {
singlePageApplication {
applicationRoute = "/admin"
useResources = true
filesPath = "admin_app"
}
}
when i try to set a route i get this
Admin_Dash_Compose.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
so i need to change my folder path to match or something? i tryed a few things but they didnt work out for me so farDenis A
04/13/2022, 4:43 PMdyld: Symbol not found: _OBJC_CLASS_$_NSURLSessionWebSocketMessage
Referenced from: /Users/my name/Library/Developer/CoreSimulator/Devices/0B611991-66BF-4BE7-9534-628D49250D1E/data/Containers/Bundle/Application/E6ED3D75-FBF2-427E-8B82-F6E9E0B09D33/App.app/Frameworks/shared.framework/shared
Expected in: /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation
in /Users/my user/Library/Developer/CoreSimulator/Devices/0B611991-66BF-4BE7-9534-628D49250D1E/data/Containers/Bundle/Application/E6ED3D75-FBF2-427E-8B82-F6E9E0B09D33/App.app/Frameworks/shared.framework/shared
dyld: launch, loading dependent libraries
DYLD_FRAMEWORK_PATH=/Users/denis.alexandrov/Library/Developer/Xcode/DerivedData/App-dljtddjulmigkpgpribvzgwhjcds/Build/Products/Debug-iphonesimulator
DYLD_FALLBACK_LIBRARY_PATH=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib
DYLD_ROOT_PATH=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot
DYLD_FALLBACK_FRAMEWORK_PATH=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks
DYLD_INSERT_LIBRARIES=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/Developer/Library
(lldb)
Anton Afanasev
04/13/2022, 4:55 PMinternal fun defaultHttpClient(): HttpClient = HttpClient {
install(HttpTimeout) {
connectTimeoutMillis = 10
requestTimeoutMillis = 10
}
}
ktor 1.6.4Android75
04/13/2022, 5:06 PMEvan
04/13/2022, 11:38 PM2.0.0
?
Ktor 2.0.0-beta-1
works on iOS 12, but 2.0.0
does not, it crashes with dyld: Symbol not found: _OBJC_CLASS_$_NSURLSessionWebSocketMessage
I’ve tried both the Darwin engine and CIO.napperley
04/14/2022, 12:53 AMAndré Martins
04/14/2022, 9:07 AMjava.io.EOFException: Failed to parse HTTP response: unexpected EOF
Can’t seem to understand further what happened but from my understanding it seems like it tried to parse the response payload but there wasn’t any.Giuliopime
04/14/2022, 9:36 AMclient = HttpClient(CIO) {
install(ContentNegotiation) {
jackson()
}
}
gnu
04/14/2022, 12:39 PMNathan Kleinschmidt
04/14/2022, 3:04 PMJames Ward
04/14/2022, 8:14 PMUncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
at kfun:kotlin.Throwable#<init>(kotlin.String?){} (0x43c461)
at kfun:kotlin.Exception#<init>(kotlin.String?){} (0x435e6d)
at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} (0x43600d)
at kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} (0x46606d)
at ThrowIncorrectDereferenceException (0x483fbe)
at CheckGlobalsAccessible (0x603112)
at kfun:io.ktor.client.engine.curl.<get-curlApi>#internal (0x8b15c7)
Seems like this has come up in the past:
https://github.com/ktorio/ktor/issues/1559
https://github.com/ktorio/ktor/pull/2193
Think I should file a new issue? Is there a known workaround?Srki Rakic
04/14/2022, 9:59 PMGET /metrics
endpoint on a different port?Srki Rakic
04/14/2022, 9:59 PMGET /metrics
endpoint on a different port?Andreas Scheja
04/14/2022, 10:14 PMSrki Rakic
04/14/2022, 10:28 PM