I think I miss something here... What is the prop...
# ktor
s
I think I miss something here... What is the proper way to send a Kotlin ByteArray from a client to a server? This is what I tried:
Copy code
val client = HttpClient()

val response: HttpResponse = <http://client.post|client.post>("<http://localhost:8080/upload>") {
    contentType(ContentType.Application.OctetStream)
    setBody(bytes)
}
Copy code
fun Application.configureRouting() {

    routing {

        get("/") {
            call.respondText("<http://stefan-oltmann.de|stefan-oltmann.de>")
        }

        post("/upload") {

            val byteArray = call.receiveStream().readBytes()

            println("Received bytes: ${byteArray.size}")

            val saveGame = SaveGameReader.readSaveGame(byteArray)

            val summary = saveGame.createSummary()

            call.respondText(summary.toString())
        }
    }
}
I run into this error:
Copy code
2024-01-01 23:15:39.439 [eventLoopGroupProxy-4-1] TRACE io.ktor.routing.Routing - Trace for [upload]
/, segment:0 -> SUCCESS @ /
  /, segment:0 -> SUCCESS @ /
    /(method:GET), segment:0 -> FAILURE "Selector didn't match" @ /(method:GET)
  /upload, segment:1 -> SUCCESS @ /upload
    /upload/(method:POST), segment:1 -> FAILURE "Selector didn't match" @ /upload/(method:POST)
  /health, segment:0 -> FAILURE "Selector didn't match" @ /health
Matched routes:
  No results
Route resolve result:
  FAILURE "No matched subtrees found" @ /
1
h
You are doing a
POST
to a
GET
endpoint. 🙂
s
This is a GET endpoint? 🤔
Copy code
post("/upload")
h
Ah,I was looking at the one above. Sorry about that, my fingers were quicker than my brain ... But:
/upload/(method:POST), segment:1 -> FAILURE "Selector didn't match" @ /upload/(method:POST)
This seems to have added a trailing slash to your request path. Could be related to that slash or it could be related to the content-type in the headers. I have found ktor to be picky by default, so not choosing xml-handling endpoints for xml-requests if not all headers are OK. Try adding an Accept-header for what the endpoint returns and see if anything changes ...
a
Is the request with another client (Postman, curl) successful?
s
Yes
h
I'd have a look at the request headers then ... Maybe you should give it a try with
Accept: **/**
in the ktor client and see if that changes anything?
s
Nevermind, I think Ktor is not so well suited as a server. I'll go with Quarkus for my pet project. Thank you.
h
🙂