Does anyone know how to create the RequestInit? An...
# javascript
j
Does anyone know how to create the RequestInit? An example will be wonderful
🧵 3
a
@turansky ^^
j
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
You need
jso
factory function instead
Copy code
val init: RequestInit = jso {
    method = ...
}
j
Thanks a lot. I have solved the doubt I had
h
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
For
headers
- Headers can be used
h
Oh nice, thank you.
t
AbortSignal
support - only in
kotlin-web
(here)