Renaud
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)
}
Aleksei Tirman [JB]
04/22/2022, 2:15 PMContentNegotiation
plugin into the client to serialize the request body:
val client = createClient {
install(ContentNegotiation) {
json()
}
}
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)
Renaud
04/22/2022, 2:26 PMval client = createClient {
this@testApplication.install(ContentNegotiation)
...
But since I’m using HOCON config file, according to the doc:
Add modules automatically
If you have thefile in theapplication.conf
folder,resources
loads all modules and properties specified in the configuration file automatically.testApplication
phldavies
04/22/2022, 2:27 PMContentNegotiation
for the client, not the server versionphldavies
04/22/2022, 2:29 PMimport io.ktor.client.plugins.contentnegotiation.ContentNegotiation
or
createClient {
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation)
}
phldavies
04/22/2022, 2:30 PMRenaud
04/22/2022, 3:08 PMRaman Sharma
01/10/2024, 1:42 PMval client by lazy {
httpClient ?: HttpClient(defaultPlatformEngine) {
// Configuring HTTP client settings
expectSuccess = true
install(ContentNegotiation) {
// Configuring JSON serialization for content negotiation
json(json)
}
defaultRequest {
// Setting default base URL for requests
url(networkConfig.baseUrl)
}
if (networkConfig.isLoggingEnabled) {
// Install logging if enabled in NetworkConfig
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
println("KTOR_LOGGER-->>\n$message")
}
}
level = LogLevel.ALL
}
}
}
}
when I am trying to call an get api, it works fine. But when I am trying post api, it throws an exception:
java.lang.NullPointerException
java.lang.NullPointerException
190424.931 W at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invokeSuspend(ContentNegotiation.kt:115)
at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:13)
at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:4)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
Please help.