Hello! I have the following problem:
I need to consume a REST API in my project, but I can’t do it in the WebAssembly (WASM) module. I’ve been trying with the following code:
suspend fun fetchApiData(url: String): String {
println("Fetching data from $url")
val response: Response = window.fetch(url).await()
if (response.ok) {
println("Getting data")
val data: String = response.text().await()
return data
} else {
throw Exception("Failed to fetch data: ${response.statusText}")
}
}
The problem is that response.text()
could be response.json()
because the API returns a JSON. However, in both cases, I encounter the following error:
Uncaught (in promise) RuntimeError: illegal cast
Thank you in advance for your help.