Need http client (post) example


I need to know how to call http post rest api, from Kotlin for JavaScript. I found one Axios example, but found no POST example. How should I do that? Thanks for giving me a hand.

I know it’s a bit late, but here is a simple file post. This one uses coroutines for “await” but you don’t really need that, you could just add your callback to promise. Also, this uploads a file but you can do whatever you want there.

import kotlinx.coroutines.await
import org.w3c.fetch.Headers
import org.w3c.fetch.RequestInit
import org.w3c.files.File
import org.w3c.xhr.FormData
import kotlin.browser.window

object Stuff {

    suspend fun post(file: File): Int {
        val body = FormData()
        body.append("file", file)

        val requestInit = RequestInit(
            body = body,
            method = "POST"

        val responsePromise = window.fetch("/some-url", requestInit)
        val response = responsePromise.await()
        // TODO handle errors... maybe :)

        val textPromise = response.text()
        val text = textPromise.await()

        return text.toInt()