This message was deleted.
# multiplatform
s
This message was deleted.
s
Can you share the ktor function call as well?
Illegal input: Unexpected JSON token at offset 0: Expected start of the object '{', but had '[' instead at path: $
This means that you're failing to deserialize the server's response. In other words, your
ProductListApiResponse
doesn't match the API's actual return types
r
Copy code
const val BASE_URL = "<https://api.escuelajs.co/api/v1/>"

val httpClient = HttpClient {
    install(ContentNegotiation) {
        json(
            Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
            }
        )
    }

    install(Logging) {
        logger = object : Logger {
            override fun log(message: String) {
                println(message)
            }
        }
        level = LogLevel.ALL
        sanitizeHeader { header ->
            header == HttpHeaders.Authorization
        }
    }
    defaultRequest {
        contentType(ContentType.Application.Json)
    }
}
s
If I had to guess, you're trying to deserialize into
ProductListApiResponse
. That would give the data format:
Copy code
{ products: [ { id, title, ..... }, { id, title, ..... } ] }
But the API is returning just the inner element, a list of products
Copy code
[ { id, title, ..... }, { id, title, ..... } ]
How are you calling
httpClient
? What does that return signature look like?
r
@Seri yes but don't understand why.
the api doesn't have a "key" products, returns [(),()]
Copy code
class FeedRemoteDataSourceImpl(
    private val httpClient: HttpClient,
) : FeedRemoteDataSource {
    override suspend fun getProductsList(): List<ProductItem> {
        val httpResponse = httpClient.get("${BASE_URL}products?offset=0&limit=1")
        // log the response in the console
        println("httpResponse: $httpResponse")
        val productListApiResponse = httpResponse.body<ProductListApiResponse>()
        println("productListApiResponse: $productListApiResponse")
        //return emptyList()
        return productListApiResponse.products.mapNotNull { it.toProductItem() }
    }
}
s
val productListApiResponse = httpResponse.body<ProductListApiResponse>()
Change this to the following:
val productListApiResponse = httpResponse.body<List<ProductApiItem>>()
You can delete your
ProductListApiResponse
class, it's not needed
r
ok...i have to map a List of ApiItems to Items
Copy code
val productListApiResponse = httpResponse.body<List<ProductListApiResponse>>()
println("productListApiResponse: $productListApiResponse")
//return emptyList()
return productListApiResponse
correct ? thanks
s
only
httpResponse.body<List<ProductApiItem>>()
is necessary
🙌 1
r
yes got it @Seri many thanks for the assistance 🙌👍
🎉 1
c
Why did you delete the post? Now all context is lost that others might be interested in 🤦🏼