Ktor HTTP client implementation for JavaScript platform providing asynchronous HTTP client functionality specifically tailored for JavaScript environments.
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-js@3.2.0Ktor Client JS is a JavaScript/TypeScript HTTP client implementation that provides asynchronous HTTP client functionality specifically tailored for JavaScript environments. Part of the Ktor framework, this module enables developers to make HTTP requests from Kotlin/JS applications using the native Fetch API in browsers and Node.js environments.
build.gradle.kts:
implementation("io.ktor:ktor-client-js:3.2.0")import io.ktor.client.*
import io.ktor.client.engine.js.*For convenience factory function:
import io.ktor.client.engine.js.JsClientimport io.ktor.client.*
import io.ktor.client.engine.js.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
// Create client with JS engine
val client = HttpClient(Js)
// or using convenience function
val client = JsClient()
// Make HTTP request
val response: HttpResponse = client.get("https://api.example.com/data")
val content: String = response.bodyAsText()
// Configure with custom options
val client = HttpClient(Js) {
engine {
configureRequest {
// Custom fetch options like credentials, cache, etc.
credentials = "include"
cache = "no-cache"
}
}
}
client.close()Ktor Client JS is built around several key components:
Js engine factory that creates HTTP clients using the Fetch APIJsClientEngineConfig for customizing engine behavior and fetch optionsJsError class for wrapping JavaScript runtime errorsCore JavaScript HTTP client engine using the Fetch API for executing requests with full coroutine support.
object Js : HttpClientEngineFactory<JsClientEngineConfig>
fun JsClient(): HttpClientEngineFactory<JsClientEngineConfig>Configuration system for customizing JavaScript client behavior, fetch options, and platform-specific settings.
open class JsClientEngineConfig : HttpClientEngineConfig() {
fun configureRequest(block: RequestInit.() -> Unit)
}Extensions for configuring individual HTTP requests with custom fetch options and JavaScript-specific parameters.
fun HttpRequestBuilder.fetchOptions(block: RequestInit.() -> Unit)JavaScript-specific error handling with wrapped native JavaScript errors for debugging and error reporting.
class JsError(val origin: dynamic) : ThrowableComplete TypeScript-compatible type definitions for the Fetch API, DOM interfaces, and JavaScript runtime types.
interface Request
interface Response
interface Headers
interface RequestInit
external fun fetch(input: String, init: RequestInit? = definedExternally): Promise<Response>