Rescribet
08/02/2021, 7:55 AMio.ktor.http.URL
isn’t @Serializable
?
Seems like a good way to avoid primitive obsession▾
@Serializable
data class Person(
val name: String,
val friends: String,
)
val p = Person("Abbey", "<http://ex.com/abbey/friends>")
Json.encodeToString(p) // { "name": "Abbey", "friends": "<http://ex.com/abbey/friends>" }
val err = """
{ "name": "Abbey", "friends": "abbey/friends" }
"""
Json.decodeFromString<Person>(err) // MalformedUrlException : SerializationException
jean
08/03/2021, 8:41 AMapplication.conf
, I declared the following :
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ ... ]
}
jwt {
secret = "secret"
issuer = "<http://0.0.0.0:8080/>"
audience = "<http://0.0.0.0:8080/login>"
realm = "Access to 'login'"
}
pbkdf2 {
salt = "salt"
iterations = "10000"
keyLength = "512"
}
}
when I use environment.config.property("jwt.secret").getString()
from Application.kt
I receive this error
Caused by: io.ktor.config.ApplicationConfigurationException: Property jwt.secret not found.What am I doing wrong here?
coder82
08/03/2021, 2:02 PMJgafner
08/03/2021, 2:45 PMVáclav Beneš
08/03/2021, 9:00 PMDanish Ansari
08/04/2021, 5:50 AMList<Stirng>
in a GET
request what's the most efficient way to do so?
I'm currently doing like this but was hoping if I can receive List<String> without any manual operation
val keywords = call.request.queryParameters["keywords"]?.split(',') // List<String>?
GET
url will be like ?keywords=a,b,c
Ali Khaleqi Yekta
08/04/2021, 11:53 AMborisdamato
08/04/2021, 2:42 PMJgafner
08/04/2021, 5:51 PMkey : [
{
aKey: aValue
},
{
bKey: bValue
}
]
....
IJ suggesting me one of those but I am not sure which one to useJeff
08/05/2021, 5:17 AMjozefdransfield
08/06/2021, 8:40 AMFernando Avanzo
08/06/2021, 2:01 PMHadi
08/07/2021, 8:29 AMroute("{year}") {
get {
val year = call.parameters["year"]!!.toIntOrNull()
if (year == null) {
call.respondText { "Year can only be an integer" }
} else {
val posts = PostRepo.getByDate(year)
call.respondHtml { respondWithPostList(posts) }
}
}
}
fun respondWithPostList(posts: List<PostDTO>?) {
HtmlContent {
head { title { +"Posts" } }
body {
posts?.forEach { post ->
run {
div {
h3 {+"${post.title} (${post.id})"}
p {+post.author.email}
}
}
}
}
}
}
But it does not work. However, the following works:
route("{year}") {
get {
val year = call.parameters["year"]!!.toIntOrNull()
if (year == null) {
call.respondText { "Year can only be an integer" }
} else {
val posts = PostRepo.getByDate(year)
call.respondHtml {
head { title { +"Posts" } }
body {
posts?.forEach { post ->
run {
div {
h3 {
+"${post.title} (${post.id})"
}
p {
+post.author.email
}
}
}
}
}
}
}
}
}
Does anyone know what I do wrong? what is the correct way to produce HTML in a function?Gerard de Leeuw
08/07/2021, 1:09 PMHtmlContent {
head { title { +"Posts" } }
}
Here you are effectively calling the constructor of HtmlContent and adding content to it, but you are not returning the constructed instance. Your function respondWithPostList
has no return type, so it will implicitly return Unit
.
I think the most neat solution is to make your respondWithPostList
function an extension method of HtmlContent
. So it would look like this:
fun HtmlContent.respondWithPostList(posts: List<PostDTO>?) {
head { title { +"Posts" } }
body {
posts?.forEach { post ->
run {
div {
h3 {+"${post.title} (${post.id})"}
p {+post.author.email}
}
}
}
}
}
okarakose
08/07/2021, 6:31 PMJonathan Hollingsworth
08/08/2021, 12:14 AMbuild.gradle.kts
file.
When I do that, the ./gradlew build
goes from passing, to failing with
Could not determine the dependencies of task ':distTar'.
> Could not resolve all dependencies for configuration ':runtimeClasspath'.
> Could not create task ':compileKotlin'.
> Could not create task of type 'KotlinCompile'.
> Could not create an instance of type org.jetbrains.kotlin.gradle.tasks.KotlinJavaToolchainProvider.
> Could not generate a decorated class for type KotlinJavaToolchainProvider.
> Cannot have abstract method KotlinJavaToolchain.getJdk().
Totally flummoxed me, and my google-fu has failed me.
Can anyone point me in the right direction please?Hadi
08/08/2021, 8:08 AMroute("") {
route("/{year}/{month?}/{day?}") {
get {
call.respondHtml{
title { +"title" }
body {
a (href = "$baseUrl/somepath") { +"link to top level" }
}
}
}
}
}
In this example, if I do href="/somepath"
, it will go to different places if month or day is given.jmfayard
08/08/2021, 5:40 PMRafs
08/08/2021, 8:59 PMjeff
08/09/2021, 1:26 AM/static/myproject.js
with no way to be more granular?Matthieu Stombellini
08/09/2021, 10:52 AMrouting {}
block is not an option). It seems that intercepting the Fallback
pipeline would be a good solution, but it turns out that a response is already sent earlier in that pipeline by default due to this code: https://github.com/ktorio/ktor/blob/066d0c4f12debb32b3e7385ca07c4f1bd767e0a4/ktor-[…]t-common/jvm/src/io/ktor/server/engine/BaseApplicationEngine.kt, thus triggering a ResponseAlreadySentException when I try to call call.respond(...)
in my interceptor.
Is there anyway to intercept the pipeline before the default interceptors kick in, or if not, which phase should I intercept?Piotr Krzemiński
08/09/2021, 12:04 PMAleksandr Ivanov
08/09/2021, 2:07 PMclient_mpp
example and found that for ByteArray with size 100 problem is not happened, but for size 200 I got an exception.
here is the code of modified ApplicationApi
@SharedImmutable
internal expect val ApplicationDispatcher: CoroutineDispatcher
class ApplicationApi {
// 100 - ok, 200 - kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen <object>
private val SIZE = 200;
private val client = HttpClient()
private val address = Url("<https://cors-test.appspot.com/test>")
fun about(callback: (String) -> Unit) {
GlobalScope.launch(ApplicationDispatcher) {
val result: String = <http://client.post|client.post> {
url(address.toString())
val filename = "test"
val mimeType = "image/png"
val filePart = formData {
val key = "photo"
val headers = Headers.build {
append(HttpHeaders.ContentDisposition, "filename=$filename;type=$mimeType")
}
append(key, ByteArray(SIZE), headers)
}
body = MultiPartFormDataContent(filePart)
}
callback(result)
}
}
}
csieflyman
08/10/2021, 4:11 AMMatias Reparaz
08/11/2021, 2:06 PMcolintheshots
08/11/2021, 2:40 PMLuca Gentile
08/11/2021, 3:43 PMcall.receive<FooRequest>()
to sanitize all fields declared as String in FooRequest
What would you suggest?Nikola Milovic
08/12/2021, 11:40 AMval data = JSONObject()
data.put("email", "email")
data.put("name", "testname")
data.put("companyName", "testcompname")
return singleFromCoroutine {
try {
val response: HttpResponse =
<http://client.post|client.post>("${serverUrl}${apiEndpoint}/account") {
body = data.toString()
contentType(ContentType.Application.Json)
}
Not sure what is happening, I'll post the entire request that gets received by the serverDMITRY.
08/12/2021, 7:32 PMHenry
08/12/2021, 9:12 PMHenry
08/12/2021, 9:12 PMAleksei Tirman [JB]
08/13/2021, 8:42 AM