While uploading file via Ktor we are facing this `...
# ktor
s
While uploading file via Ktor we are facing this
NullPointerException
can someone please help to know what are we missing on implementation, here is the sample code
Copy code
fun uploadFile(url: String, filename: String, key: String = "json"): HttpResponse {
    return runBlocking {
        val headers = Headers.build {
            append(HttpHeaders.ContentType, ContentType.Application.Json)
            append(HttpHeaders.ContentDisposition, "filename=$filename")
        }

        val response: HttpResponse = httpClient.submitFormWithBinaryData(
            url = url,
            formData = formData {
                append(key, File(filename).readBytes(), headers)
            }
        ) {
            onUpload { bytesSentTotal, contentLength ->
                println("Sent $bytesSentTotal bytes from $contentLength")
            }
            method = HttpMethod.Put
        }

        println(response.readText())
        return@runBlocking response
    }
}
a
Can you share the stack trace as text?
s
stack.rtf
a
Untitled
I can't see the
NullPointerException
in the stack trace.
s
Copy code
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  java.lang.NullPointerException
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.ContentNegotiation$Plugin$install$1.invokeSuspend(ContentNegotiation.kt:109)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:13)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:4)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:88)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:130)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(Unknown Source:13)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(Unknown Source:4)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invokeSuspend(HttpRequestLifecycle.kt:38)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(Unknown Source:11)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(Unknown Source:4)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2023-11-30 17:40:29.566 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:98)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.HttpClient.execute$ktor_client_core(HttpClient.kt:191)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:108)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:47)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:62)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at utils.fileupload.FileUploadServiceImpl.uploadFile(FileUploadServiceImpl.kt:105)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at utils.fileupload.FileUploadRepositoryImp$upload$2.invokeSuspend(FileUploadRepositoryImp.kt:17)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
2023-11-30 17:40:29.567 31430-31457 System.out              xyz.penpencil.unigo                  I  	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
2023-11-30 17:40:29.568 31430-31430 System.out              xyz.penpencil.unigo                  I  Suresh base.domain.ResourceError$Unknown@a6bb78
@Aleksei Tirman [JB] hope this helps you?
a
Can you share the
HttpClient
initialization code (
httpClient
)?
s
Copy code
module {
        single<HttpClient> {
            HttpClient {
                // converts response into dto objects
                install(ContentNegotiation) {
                    json(HttpClientUtil.nonStrictJson)
                }

                // Log HTTP request and response
                install(Logging) {
                    logger = HttpClientLogger // custom logger
                    level = LogLevel.ALL
                }.also { initLogger() } // this will initialise napier for both platform

                defaultRequest {
                    // Set the Content-Type header for each request
                    header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
                    header("Client-id", ORG_ID)
                }
            }
        }

        single<KtorApiService> { KtorApiServiceImp(get<HttpClient>()) }
    }
im using koin
a
What version of Ktor do you use? Can you try replacing the
response.readText()
line with
response.bodyAsText()
one?
s
Copy code
2.0.0-beta-1
a
Can you use the latest released version 2.3.6?
s
okay I will try both suggestion will let you know
btw im using this in KMM android and iOS project
@Aleksei Tirman [JB] thanks a lot updating ktor along with that setting up platform specific http client to ktor helped
Copy code
HttpClient(engine = getHttpClientEngine())
ios
Copy code
actual fun getHttpClientEngine() = Darwin.create()
android
Copy code
actual fun getHttpClientEngine() = OkHttp.create {

}
@Aleksei Tirman [JB] we are started seeing new issue when we run build on iOS but android build works fine https://kotlinlang.slack.com/archives/C0A974TJ9/p1701410061728279