Hey. I am writing a backend code in kotlin/microna...
# ktor
s
Hey. I am writing a backend code in kotlin/micronaut. The scenario which I am facing is that my backend will recieve a PDF file from client and I have to redirect that PDF file to another endpoint(This endpoint is in another service). So to deal this I am using Ktor. But it is throwing me an error. My code is
Copy code
import io.ktor.client.HttpClient
import io.ktor.client.request.forms.*
import <http://io.ktor.client.request.post|io.ktor.client.request.post>
import io.ktor.http.*
import io.ktor.util.*
import io.micronaut.http.HttpResponse
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import <http://io.micronaut.http.annotation.Post|io.micronaut.http.annotation.Post>
import io.micronaut.http.multipart.CompletedFileUpload
import io.micronaut.security.annotation.Secured
import io.micronaut.security.rules.SecurityRule

@Secured(SecurityRule.IS_ANONYMOUS)
@Controller("/pdf-extract")
class PdfService(private val ktorClient: HttpClient) {

    @Post(value = "/", consumes = [MediaType.MULTIPART_FORM_DATA], produces = [MediaType.APPLICATION_JSON])
    suspend fun pdfService(@Body files: CompletedFileUpload): String {
        val response = uploadFileToExternalService(files)
        return if (response.code() == 200) {
            "Response from API: ${response.body()}"
        } else {
            "Failed to fetch data from API"
        }
    }

    @OptIn(InternalAPI::class)
    suspend fun uploadFileToExternalService(file: CompletedFileUpload): HttpResponse<String> {
        return try {
            val response = <http://ktorClient.post|ktorClient.post><String>("<http://127.0.0.1:8000/file/>") {
                body = MultiPartFormDataContent(formData {
                    append("file", file.inputStream, Headers.build {
                        append(HttpHeaders.ContentType, file.contentType.toString())
                        append(HttpHeaders.ContentDisposition, "filename=${file.name}")
                    })
                })
            }
            HttpResponse.ok(response)
        } catch (e: Exception) {
            HttpResponse.serverError("Failed to upload file: ${e.message}")
        }
    }
}
The error message which I am getting is
Copy code
Failed to inject value for parameter [ktorClient] of class: dk.controllers.PdfService\r\n\r\nMessage: No bean of type [io.ktor.client.HttpClient] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).\r\nPath Taken: new PdfService(HttpClient ktorClient) --> new PdfService([HttpClient ktorClient])"
a
Seems like this error is Micronaut-specific. Are you able to solve your problem with just Ktor?
s
Nope, I require my micronaut code for receiving the request to my backend. Then I am using Ktor for redirecting this request to another service. I think this error is related to some dependency injection but I am not able to figure it out that how to solve it