https://kotlinlang.org logo
#javascript
Title
# javascript
j

Jaime Vizan

12/01/2023, 4:58 PM
Does anyone know how to create the RequestInit? An example will be wonderful
🧵 3
a

Artem Kobzar

12/01/2023, 5:19 PM
@turansky ^^
j

Jaime Vizan

12/01/2023, 5:54 PM
I've used the following
Copy code
init = js("{" +
        "\"method\": \"GET\"," +
        "\"headers\": {" +
        "\"Accept\": \"application/json\""+
        "}" +
        "}"))
But I don't know if it is the best way to define this object
t

turansky

12/01/2023, 6:20 PM
You need
jso
factory function instead
Copy code
val init: RequestInit = jso {
    method = ...
}
j

Jaime Vizan

12/01/2023, 6:46 PM
Thanks a lot. I have solved the doubt I had
h

Hildebrandt Tobias

12/03/2023, 12:17 AM
Here a little snipped from what I use:
Copy code
fun newTokenRequest(
    code: String,
    redirectUri: String,
): Promise<Response>{
    val fetchURL = "${environment.KEYCLOAK_URL}/realms/${environment.KEYCLOAK_REALM}/protocol/openid-connect/token"
    val body = "grant_type=authorization_code&client_id=${environment.KEYCLOAK_CLIENT}&code=$code&redirect_uri=$redirectUri"

    val request = RequestInit(
        method = "POST",
        // The POST needs the same redirect_uri as the GET that was used to fetch the code.
        body = body,
        headers = json(
            *listOf(
                Pair("Content-Type", "application/x-www-form-urlencoded")
            ).toTypedArray()
        ),
        mode = RequestMode.CORS
    )
    return window.fetch(fetchURL, request)
}
t

turansky

12/03/2023, 12:21 AM
For
headers
- Headers can be used
h

Hildebrandt Tobias

12/03/2023, 12:22 AM
Oh nice, thank you.
t

turansky

12/03/2023, 12:24 AM
AbortSignal
support - only in
kotlin-web
(here)
2 Views