🤔 Documentation issue? Report or edit


ResponseConverter is deprecated, use Converter.ResponseConverter instead

Let`s say you have a function that requests a list of comments

suspend fun getCommentsByPostId(@Path("postId") postId: Int): List<Comment>

But now you want to directly wrap your comment list in your data holder class e.g. “MyOwnResponse”

sealed class MyOwnResponse<T> {
    data class Success<T>(val data: T) : MyOwnResponse<T>()
    class Error(val ex:Throwable) : MyOwnResponse<Nothing>()

    companion object {
        fun <T> success(data: T) = Success(data)
        fun error(ex: Throwable) = Error(ex)

To enable that, you have to implement a ResponseConverter. This class will be used to wrap the Ktor response inside your wrapper class.

class MyOwnResponseConverter : ResponseConverter {

    override suspend fun <RequestType> wrapResponse(
        typeData: TypeData,
        requestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,
        ktorfit: Ktorfit
    ): Any {
        return try {
            val (info, response) = requestFunction()
        } catch (ex: Throwable) {

    override fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {
        return typeData.qualifiedName == "com.example.model.MyOwnResponse"

You can then add the ResponseConverter on your Ktorfit object.


Now add MyOwnResponse to your function

suspend fun getCommentsByPostId(@Path("postId") postId: Int): MyOwnResponse<List<Comment>>

Last update: May 27, 2023