Ktor HTTP client core library providing asynchronous HTTP client capabilities for Kotlin multiplatform applications
—
Prepared statement functionality for reusable HTTP requests with lazy execution, response handling, and type-safe body parsing. HTTP statements provide a way to prepare requests and execute them multiple times with different execution contexts.
Core statement class for preparing and executing HTTP requests.
/**
* Prepared HTTP request statement for lazy execution
* @param builder - Request builder configuration
* @param client - HTTP client for execution
*/
class HttpStatement(
val builder: HttpRequestBuilder,
val client: HttpClient
) {
/** Execute request and return response */
suspend fun execute(): HttpResponse
/** Execute request with response handler */
suspend fun <T> execute(block: suspend (response: HttpResponse) -> T): T
/** Execute request and receive typed body */
suspend fun <T> body(): T
/** Execute request, receive typed body, and process it */
suspend fun <T, R> body(block: suspend (response: T) -> R): R
}Usage Examples:
import io.ktor.client.*
import io.ktor.client.statement.*
val client = HttpClient()
// Create a prepared statement
val statement = client.prepareGet("https://api.example.com/users")
// Execute multiple times
val response1 = statement.execute()
val response2 = statement.execute()
// Execute with handler
val processedData = statement.execute { response ->
if (response.status.isSuccess()) {
response.bodyAsText()
} else {
"Error: ${response.status}"
}
}
// Execute and get typed body
val users: List<User> = statement.body()
// Execute, get typed body, and process
val userCount = statement.body<List<User>> { users ->
users.size
}Execute prepared statements with various response handling strategies.
/**
* Execute request and return response
* @return HttpResponse for manual processing
*/
suspend fun execute(): HttpResponse
/**
* Execute request with response handler
* @param block - Response processing block
* @return Processed result from block
*/
suspend fun <T> execute(block: suspend (response: HttpResponse) -> T): TType-safe response body parsing with optional processing.
/**
* Execute request and receive typed body
* @return Deserialized response body
*/
suspend fun <T> body(): T
/**
* Execute request, receive typed body, and process it
* @param block - Body processing block
* @return Processed result from block
*/
suspend fun <T, R> body(block: suspend (response: T) -> R): RExtension functions for creating prepared statements from HTTP clients.
// Prepare request methods
suspend fun HttpClient.prepareRequest(
builder: HttpRequestBuilder
): HttpStatement
suspend fun HttpClient.prepareRequest(
block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement
suspend fun HttpClient.prepareRequest(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement
// HTTP method-specific preparation
suspend fun HttpClient.prepareGet(urlString: String): HttpStatement
suspend fun HttpClient.preparePost(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement
suspend fun HttpClient.preparePut(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement
suspend fun HttpClient.prepareDelete(urlString: String): HttpStatement
suspend fun HttpClient.prepareHead(urlString: String): HttpStatement
suspend fun HttpClient.prepareOptions(urlString: String): HttpStatement
suspend fun HttpClient.preparePatch(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
): HttpStatementAdvanced Usage Examples:
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
val client = HttpClient()
// Prepare POST request with body
val postStatement = client.preparePost("https://api.example.com/users") {
header("Content-Type", "application/json")
setBody("""{"name": "John", "email": "john@example.com"}""")
}
// Execute with error handling
val result = postStatement.execute { response ->
when {
response.status.isSuccess() -> {
val user: User = response.body()
"Created user: ${user.name}"
}
response.status.value == 409 -> {
"User already exists"
}
else -> {
"Error: ${response.status.description}"
}
}
}
// Reuse statement with different processing
val userId = postStatement.body<User> { user -> user.id }
val userName = postStatement.body<User> { user -> user.name }Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-core-jvm