Ktor HTTP provides comprehensive HTTP utilities for the Ktor framework, specifically compiled for iOS ARM64 platform. This library contains all the fundamental HTTP components needed for building client and server applications, including URL handling, HTTP headers, content types, authentication, cookies, and multipart data processing.
implementation("io.ktor:ktor-http-iosarm64:3.2.0")import io.ktor.http.*Specific imports for core functionality:
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.Url
import io.ktor.http.URLBuilder
import io.ktor.http.ContentType
import io.ktor.http.Headers
import io.ktor.http.Parameters
import io.ktor.http.Cookieimport io.ktor.http.*
// Create URLs
val url = URLBuilder("https://api.example.com")
.path("users", "123")
.parameters.append("format", "json")
.build()
// Work with HTTP methods and status codes
val method = HttpMethod.Get
val status = HttpStatusCode.OK
println("$method request returned $status")
// Handle content types
val jsonContentType = ContentType.Application.Json
val htmlContentType = ContentType.Text.Html.withCharset(Charsets.UTF_8)
// Build headers
val headers = headersOf(
HttpHeaders.ContentType to jsonContentType.toString(),
HttpHeaders.UserAgent to "MyApp/1.0"
)
// Work with parameters
val params = parametersOf(
"page" to listOf("1"),
"limit" to listOf("10")
)Ktor HTTP is built around several key components that provide a complete HTTP foundation:
This modular design enables developers to use individual components independently while ensuring compatibility across the entire Ktor ecosystem.
Core HTTP protocol elements including standard methods (GET, POST, PUT, DELETE, etc.) and comprehensive status code definitions with parsing and validation.
data class HttpMethod(val value: String) {
companion object {
val Get: HttpMethod
val Post: HttpMethod
val Put: HttpMethod
val Patch: HttpMethod
val Delete: HttpMethod
val Head: HttpMethod
val Options: HttpMethod
fun parse(method: String): HttpMethod
}
}
data class HttpStatusCode(val value: Int, val description: String) {
fun description(value: String): HttpStatusCode
companion object {
val OK: HttpStatusCode
val Created: HttpStatusCode
val BadRequest: HttpStatusCode
val Unauthorized: HttpStatusCode
val NotFound: HttpStatusCode
val InternalServerError: HttpStatusCode
fun fromValue(value: Int): HttpStatusCode
}
}Comprehensive URL manipulation including parsing, building, encoding, and parameter handling with support for all URL components.
class Url {
val protocol: URLProtocol
val host: String
val port: Int
val specifiedPort: Int
val encodedPath: String
val parameters: Parameters
val fragment: String
val user: String?
val password: String?
val pathSegments: List<String>
}
class URLBuilder {
var protocol: URLProtocol
var host: String
var port: Int
var encodedPath: String
val parameters: ParametersBuilder
var fragment: String
var user: String?
var password: String?
fun appendPathSegments(vararg segments: String): URLBuilder
fun path(vararg components: String): URLBuilder
fun build(): Url
}Type-safe HTTP header and parameter handling with case-insensitive access, validation, and comprehensive builder patterns.
interface Headers : StringValues {
operator fun get(name: String): String?
fun getAll(name: String): List<String>?
fun contains(name: String): Boolean
fun contains(name: String, value: String): Boolean
}
interface Parameters : StringValues {
operator fun get(name: String): String?
fun getAll(name: String): List<String>?
}
fun headersOf(vararg pairs: Pair<String, String>): Headers
fun parametersOf(vararg pairs: Pair<String, List<String>>): ParametersRich content type system with MIME type support, character encoding, parameter handling, and comprehensive predefined content types.
class ContentType(
val contentType: String,
val contentSubtype: String,
val parameters: List<HeaderValueParam>
) {
fun withParameter(name: String, value: String): ContentType
fun withoutParameters(): ContentType
fun match(pattern: ContentType): Boolean
companion object {
fun parse(value: String): ContentType
val Any: ContentType
}
object Application {
val Json: ContentType
val Xml: ContentType
val FormUrlEncoded: ContentType
val OctetStream: ContentType
}
object Text {
val Plain: ContentType
val Html: ContentType
val CSS: ContentType
val JavaScript: ContentType
}
}HTTP authentication header parsing and generation supporting multiple authentication schemes including Basic, Bearer, Digest, and custom schemes.
sealed class HttpAuthHeader(val authScheme: String) {
fun render(): String
fun render(encoding: HeaderValueEncoding): String
class Single(authScheme: String, val blob: String) : HttpAuthHeader(authScheme)
class Parameterized(
authScheme: String,
val parameters: Map<String, String>
) : HttpAuthHeader(authScheme)
}
fun parseAuthorizationHeader(headerValue: String): HttpAuthHeader?
fun parseAuthorizationHeaders(headerValues: List<String>): List<HttpAuthHeader>Complete cookie support including parsing Set-Cookie and Cookie headers, cookie encoding strategies, and cookie attribute handling.
data class Cookie(
val name: String,
val value: String,
val encoding: CookieEncoding = CookieEncoding.URI_ENCODING,
val maxAge: Int = 0,
val expires: GMTDate? = null,
val domain: String? = null,
val path: String? = null,
val secure: Boolean = false,
val httpOnly: Boolean = false,
val extensions: Map<String, String?> = emptyMap()
)
enum class CookieEncoding {
RAW, DQUOTES, URI_ENCODING, BASE64_ENCODING
}
fun parseServerSetCookieHeader(cookiesHeader: String): Cookie
fun parseClientCookiesHeader(cookiesHeader: String): List<Cookie>
fun renderSetCookieHeader(cookie: Cookie): String
fun renderCookieHeader(cookies: List<Cookie>): StringOutgoing content abstractions for different data types including text, binary data, streaming content, and multipart form data processing.
sealed class OutgoingContent {
abstract val contentType: ContentType?
abstract val contentLength: Long?
abstract val status: HttpStatusCode?
abstract val headers: Headers
class NoContent : OutgoingContent()
class ByteArrayContent(val bytes: ByteArray) : OutgoingContent()
class TextContent(
val text: String,
override val contentType: ContentType,
override val status: HttpStatusCode? = null
) : OutgoingContent()
}
interface MultiPartData {
suspend fun readPart(): PartData?
}
sealed class PartData {
abstract val dispose: () -> Unit
abstract val headers: Headers
abstract val name: String?
class FormItem(val value: String, /* ... */) : PartData()
class FileItem(val provider: () -> InputStream, /* ... */) : PartData()
class BinaryItem(val provider: () -> InputStream, /* ... */) : PartData()
}data class URLProtocol(val name: String, val defaultPort: Int) {
companion object {
val HTTP: URLProtocol
val HTTPS: URLProtocol
val WS: URLProtocol
val WSS: URLProtocol
}
}
interface StringValues {
operator fun get(name: String): String?
fun getAll(name: String): List<String>?
fun names(): Set<String>
fun isEmpty(): Boolean
fun entries(): Set<Map.Entry<String, List<String>>>
fun forEach(body: (String, List<String>) -> Unit)
}
data class HeaderValueParam(val name: String, val value: String)
interface HttpMessage {
val headers: Headers
}fun String.encodeURLQueryComponent(spaceToPlus: Boolean = false): String
fun String.encodeURLPathPart(): String
fun String.encodeURLPath(encodeSlash: Boolean = false): String
fun String.encodeURLParameter(spaceToPlus: Boolean = false): String
fun String.encodeOAuth(): Stringclass URLDecodeException(message: String) : Exception(message)
class URLParserException(urlString: String, cause: Throwable) : IllegalStateException()
class BadContentTypeFormatException(value: String) : Exception()
class UnsafeHeaderException(header: String) : IllegalArgumentException()
class IllegalHeaderNameException(val headerName: String, val position: Int) : IllegalArgumentException()
class IllegalHeaderValueException(val headerValue: String, val position: Int) : IllegalArgumentException()