JavaScript/WebAssembly implementation of Ktor's HTTP core library providing HTTP utilities, URL building, request/response handling, and HTTP method definitions for multiplatform Kotlin applications targeting JavaScript environments
—
Essential HTTP protocol components including methods, status codes, protocol versions, and message handling interfaces.
Represents HTTP request methods with common methods pre-defined and parsing support.
/**
* Represents an HTTP request method
* @param value The string representation of the HTTP method
*/
class HttpMethod(val value: String) {
fun component1(): String = value
fun copy(value: String = this.value): HttpMethod
companion object {
val Get: HttpMethod
val Post: HttpMethod
val Put: HttpMethod
val Delete: HttpMethod
val Head: HttpMethod
val Options: HttpMethod
val Patch: HttpMethod
val DefaultMethods: List<HttpMethod>
/**
* Parse HTTP method from string
* @param method Method string (case-sensitive)
* @return HttpMethod instance
*/
fun parse(method: String): HttpMethod
}
}
/**
* Check if HTTP method supports request body
*/
val HttpMethod.supportsRequestBody: BooleanUsage Examples:
import io.ktor.http.HttpMethod
// Use predefined methods
val getMethod = HttpMethod.Get
val postMethod = HttpMethod.Post
// Parse from string
val customMethod = HttpMethod.parse("PATCH")
// Check body support
val supportsBody = HttpMethod.Post.supportsRequestBody // true
val noBody = HttpMethod.Get.supportsRequestBody // falseRepresents HTTP response status codes with common codes pre-defined and utility functions.
/**
* Represents an HTTP status code
* @param value The numeric status code
* @param description Human-readable description
*/
data class HttpStatusCode(val value: Int, val description: String) : Comparable<HttpStatusCode> {
fun description(description: String): HttpStatusCode
companion object {
// 1xx Informational
val Continue: HttpStatusCode
val SwitchingProtocols: HttpStatusCode
val Processing: HttpStatusCode
// 2xx Success
val OK: HttpStatusCode
val Created: HttpStatusCode
val Accepted: HttpStatusCode
val NonAuthoritativeInformation: HttpStatusCode
val NoContent: HttpStatusCode
val ResetContent: HttpStatusCode
val PartialContent: HttpStatusCode
val MultiStatus: HttpStatusCode
// 3xx Redirection
val MultipleChoices: HttpStatusCode
val MovedPermanently: HttpStatusCode
val Found: HttpStatusCode
val SeeOther: HttpStatusCode
val NotModified: HttpStatusCode
val UseProxy: HttpStatusCode
val SwitchProxy: HttpStatusCode
val TemporaryRedirect: HttpStatusCode
val PermanentRedirect: HttpStatusCode
// 4xx Client Error
val BadRequest: HttpStatusCode
val Unauthorized: HttpStatusCode
val PaymentRequired: HttpStatusCode
val Forbidden: HttpStatusCode
val NotFound: HttpStatusCode
val MethodNotAllowed: HttpStatusCode
val NotAcceptable: HttpStatusCode
val ProxyAuthenticationRequired: HttpStatusCode
val RequestTimeout: HttpStatusCode
val Conflict: HttpStatusCode
val Gone: HttpStatusCode
val LengthRequired: HttpStatusCode
val PreconditionFailed: HttpStatusCode
val PayloadTooLarge: HttpStatusCode
val RequestURITooLong: HttpStatusCode
val UnsupportedMediaType: HttpStatusCode
val RequestedRangeNotSatisfiable: HttpStatusCode
val ExpectationFailed: HttpStatusCode
val UnprocessableEntity: HttpStatusCode
val Locked: HttpStatusCode
val FailedDependency: HttpStatusCode
val TooEarly: HttpStatusCode
val UpgradeRequired: HttpStatusCode
val TooManyRequests: HttpStatusCode
val RequestHeaderFieldTooLarge: HttpStatusCode
// 5xx Server Error
val InternalServerError: HttpStatusCode
val NotImplemented: HttpStatusCode
val BadGateway: HttpStatusCode
val ServiceUnavailable: HttpStatusCode
val GatewayTimeout: HttpStatusCode
val VersionNotSupported: HttpStatusCode
val VariantAlsoNegotiates: HttpStatusCode
val InsufficientStorage: HttpStatusCode
val allStatusCodes: List<HttpStatusCode>
/**
* Get status code by numeric value
* @param value HTTP status code number
* @return HttpStatusCode instance
*/
fun fromValue(value: Int): HttpStatusCode
}
}
/**
* Check if status code indicates success (2xx range)
*/
fun HttpStatusCode.isSuccess(): BooleanUsage Examples:
import io.ktor.http.HttpStatusCode
import io.ktor.http.isSuccess
// Use predefined status codes
val okStatus = HttpStatusCode.OK
val notFoundStatus = HttpStatusCode.NotFound
// Create custom status
val customStatus = HttpStatusCode(299, "Custom Success")
// Get by value
val status = HttpStatusCode.fromValue(404) // NotFound
// Check success
val isOk = HttpStatusCode.OK.isSuccess // true
val isError = HttpStatusCode.InternalServerError.isSuccess // false
// Compare status codes
val comparison = HttpStatusCode.OK < HttpStatusCode.NotFound // trueRepresents HTTP protocol versions with common versions pre-defined.
/**
* Represents HTTP protocol version
* @param name Protocol name (e.g., "HTTP")
* @param major Major version number
* @param minor Minor version number
*/
data class HttpProtocolVersion(val name: String, val major: Int, val minor: Int) {
companion object {
val HTTP_1_0: HttpProtocolVersion
val HTTP_1_1: HttpProtocolVersion
val HTTP_2_0: HttpProtocolVersion
val SPDY_3: HttpProtocolVersion
val QUIC: HttpProtocolVersion
/**
* Get or create protocol version
* @param name Protocol name
* @param major Major version
* @param minor Minor version
* @return HttpProtocolVersion instance
*/
fun fromValue(name: String, major: Int, minor: Int): HttpProtocolVersion
/**
* Parse protocol version from string
* @param value Protocol version string (e.g., "HTTP/1.1")
* @return HttpProtocolVersion instance
*/
fun parse(value: CharSequence): HttpProtocolVersion
}
}Usage Examples:
import io.ktor.http.HttpProtocolVersion
// Use predefined versions
val http11 = HttpProtocolVersion.HTTP_1_1
val http2 = HttpProtocolVersion.HTTP_2_0
// Create custom version
val customVersion = HttpProtocolVersion.fromValue("CUSTOM", 1, 0)
// Parse from string
val parsed = HttpProtocolVersion.parse("HTTP/1.1")Base interfaces for HTTP messages with headers support.
/**
* Base interface for HTTP messages
*/
interface HttpMessage {
val headers: Headers
}
/**
* Builder interface for HTTP messages
*/
interface HttpMessageBuilder {
val headers: HeadersBuilder
}Extension functions for accessing common HTTP message properties.
/**
* Get content type from HTTP message
*/
fun HttpMessage.contentType(): ContentType?
fun HttpMessageBuilder.contentType(): ContentType?
/**
* Set content type on HTTP message builder
*/
fun HttpMessageBuilder.contentType(contentType: ContentType)
/**
* Get content length from HTTP message
*/
fun HttpMessage.contentLength(): Long?
fun HttpMessageBuilder.contentLength(): Long?
/**
* Get charset from HTTP message
*/
fun HttpMessage.charset(): Charset?
fun HttpMessageBuilder.charset(): Charset?
/**
* Get cache control directives from HTTP message
*/
fun HttpMessage.cacheControl(): List<CacheControl>
/**
* Get ETag from HTTP message
*/
fun HttpMessage.etag(): String?
fun HttpMessageBuilder.etag(): String?
/**
* Get Vary header values from HTTP message
*/
fun HttpMessage.vary(): List<String>
fun HttpMessageBuilder.vary(): List<String>
/**
* Get Set-Cookie headers from HTTP message
*/
fun HttpMessage.setCookie(): List<Cookie>
/**
* Get cookies from HTTP message builder
*/
fun HttpMessageBuilder.cookies(): List<Cookie>
/**
* Set various message properties
*/
fun HttpMessageBuilder.ifNoneMatch(etag: String)
fun HttpMessageBuilder.maxAge(seconds: Int)
fun HttpMessageBuilder.userAgent(userAgent: String)Usage Examples:
import io.ktor.http.*
// Working with HTTP messages
fun processMessage(message: HttpMessage) {
val contentType = message.contentType()
val contentLength = message.contentLength()
val charset = message.charset()
val etag = message.etag()
}
// Building HTTP messages
fun buildMessage(builder: HttpMessageBuilder) {
builder.contentType(ContentType.Application.Json)
builder.userAgent("MyApp/1.0")
builder.maxAge(3600)
}Interface representing connection details for HTTP requests.
/**
* Represents connection point information for HTTP requests
*/
interface RequestConnectionPoint {
val scheme: String
val version: String
val uri: String
val method: HttpMethod
val host: String
val port: Int
val localAddress: String
val localHost: String
val localPort: Int
val remoteAddress: String
val remoteHost: String
val remotePort: Int
val serverHost: String
val serverPort: Int
}This interface is typically implemented by HTTP server frameworks to provide connection metadata.
Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-http-js