Slackbot
03/21/2025, 5:33 PMSeri
03/21/2025, 5:40 PMSeri
03/21/2025, 5:41 PMIllegal 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 typesRomão
03/21/2025, 5:41 PMconst 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)
}
}
Seri
03/21/2025, 5:43 PMProductListApiResponse
. That would give the data format:
{ products: [ { id, title, ..... }, { id, title, ..... } ] }
But the API is returning just the inner element, a list of products
[ { id, title, ..... }, { id, title, ..... } ]
Seri
03/21/2025, 5:44 PMhttpClient
? What does that return signature look like?Romão
03/21/2025, 5:45 PMRomão
03/21/2025, 5:45 PMRomão
03/21/2025, 5:46 PMclass 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() }
}
}
Seri
03/21/2025, 5:47 PMval productListApiResponse = httpResponse.body<ProductListApiResponse>()
Change this to the following:
val productListApiResponse = httpResponse.body<List<ProductApiItem>>()
You can delete your ProductListApiResponse
class, it's not neededRomão
03/21/2025, 5:52 PMval productListApiResponse = httpResponse.body<List<ProductListApiResponse>>()
println("productListApiResponse: $productListApiResponse")
//return emptyList()
return productListApiResponse
correct ? thanksSeri
03/21/2025, 6:01 PMhttpResponse.body<List<ProductApiItem>>()
is necessaryRomão
03/21/2025, 6:01 PMChrimaeon
03/22/2025, 8:35 AMChrimaeon
03/22/2025, 9:48 AM