CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-http-js

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

Pending
Overview
Eval results
Files

content-types.mddocs/

Content Types

MIME content type system with pre-defined types, parameter handling, and charset support.

Capabilities

ContentType Class

Core class representing MIME content types with parameter support.

/**
 * Represents a MIME content type with parameters
 * @param contentType Main type (e.g., "application")
 * @param contentSubtype Subtype (e.g., "json")  
 * @param parameters List of parameters (e.g., charset)
 */
class ContentType(
    val contentType: String,
    val contentSubtype: String,
    val parameters: List<HeaderValueParam> = emptyList()
) : HeaderValueWithParameters {
    
    /**
     * Check if this content type matches another (ignoring parameters)
     * @param contentType Content type to match against
     * @return true if types match
     */
    fun match(contentType: ContentType): Boolean
    
    /**
     * Check if this content type matches a string pattern
     * @param pattern String pattern (e.g., "application/*")
     * @return true if pattern matches
     */
    fun match(pattern: String): Boolean
    
    /**
     * Add or replace a parameter
     * @param name Parameter name
     * @param value Parameter value
     * @return New ContentType with parameter
     */
    fun withParameter(name: String, value: String): ContentType
    
    /**
     * Remove all parameters
     * @return New ContentType without parameters
     */
    fun withoutParameters(): ContentType
    
    companion object {
        val Any: ContentType
        
        /**
         * Parse content type from string
         * @param value Content type string (e.g., "application/json; charset=utf-8")
         * @return ContentType instance
         */
        fun parse(value: String): ContentType
    }
}

Application Content Types

Pre-defined content types for application/* MIME types.

/**
 * Application content types
 */
object ContentType.Application {
    val Any: ContentType
    val Atom: ContentType
    val Cbor: ContentType
    val Json: ContentType
    val JavaScript: ContentType
    val OctetStream: ContentType
    val FormUrlEncoded: ContentType
    val Pdf: ContentType
    val ProtoBuf: ContentType
    val Rss: ContentType
    val Xml: ContentType
    val Xml_Dtd: ContentType
    val Zip: ContentType
    val GZip: ContentType
    val Wasm: ContentType
    val ProblemJson: ContentType
    val ProblemXml: ContentType
    val HalJson: ContentType
    val Yaml: ContentType
    val Soap: ContentType
    val Docx: ContentType
    val Xlsx: ContentType
    val Pptx: ContentType
    
    /**
     * Check if content type belongs to application/*
     * @param contentType Content type to check
     * @return true if application type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: CharSequence): Boolean
}

Text Content Types

Pre-defined content types for text/* MIME types.

/**
 * Text content types
 */
object ContentType.Text {
    val Any: ContentType
    val Plain: ContentType
    val CSS: ContentType
    val CSV: ContentType
    val Html: ContentType
    val JavaScript: ContentType
    val VCard: ContentType
    val Xml: ContentType
    val EventStream: ContentType
    
    /**
     * Check if content type belongs to text/*
     * @param contentType Content type to check
     * @return true if text type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: CharSequence): Boolean
}

Image Content Types

Pre-defined content types for image/* MIME types.

/**
 * Image content types
 */
object ContentType.Image {
    val Any: ContentType
    val GIF: ContentType
    val JPEG: ContentType
    val PNG: ContentType
    val SVG: ContentType
    val XIcon: ContentType
    
    /**
     * Check if content type belongs to image/*
     * @param contentType Content type to check
     * @return true if image type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: String): Boolean
}

Audio Content Types

Pre-defined content types for audio/* MIME types.

/**
 * Audio content types
 */
object ContentType.Audio {
    val Any: ContentType
    val MP4: ContentType
    val MPEG: ContentType
    val OGG: ContentType
    
    /**
     * Check if content type belongs to audio/*
     * @param contentType Content type to check
     * @return true if audio type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: CharSequence): Boolean
}

Video Content Types

Pre-defined content types for video/* MIME types.

/**
 * Video content types
 */
object ContentType.Video {
    val Any: ContentType
    val MP4: ContentType
    val MPEG: ContentType
    val OGG: ContentType
    val QuickTime: ContentType
    
    /**
     * Check if content type belongs to video/*
     * @param contentType Content type to check
     * @return true if video type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: CharSequence): Boolean
}

Font Content Types

Pre-defined content types for font/* MIME types.

/**
 * Font content types
 */
object ContentType.Font {
    val Any: ContentType
    val Collection: ContentType
    val Otf: ContentType  
    val Sfnt: ContentType
    val Ttf: ContentType
    val Woff: ContentType
    val Woff2: ContentType
    
    /**
     * Check if content type belongs to font/*
     * @param contentType Content type to check
     * @return true if font type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: CharSequence): Boolean
}

Multipart Content Types

Pre-defined content types for multipart/* MIME types.

/**
 * Multipart content types
 */  
object ContentType.MultiPart {
    val Any: ContentType
    val Mixed: ContentType
    val Alternative: ContentType
    val Related: ContentType
    val FormData: ContentType
    val Signed: ContentType
    val Encrypted: ContentType
    val ByteRanges: ContentType
    
    /**
     * Check if content type belongs to multipart/*
     * @param contentType Content type to check
     * @return true if multipart type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: CharSequence): Boolean
}

Message Content Types

Pre-defined content types for message/* MIME types.

/**
 * Message content types
 */
object ContentType.Message {
    val Any: ContentType
    val Http: ContentType
    
    /**
     * Check if content type belongs to message/*
     * @param contentType Content type to check
     * @return true if message type
     */
    fun contains(contentType: ContentType): Boolean
    fun contains(contentTypeString: String): Boolean
}

Content Type Matching

Interface and utilities for content type matching.

/**
 * Interface for content type matching
 */
interface ContentTypeMatcher {
    /**
     * Check if content type matches this matcher
     * @param contentType Content type to check
     * @return true if matches
     */
    fun contains(contentType: ContentType): Boolean
}

Charset Utilities

Utilities for working with charset parameters in content types.

/**
 * Get charset parameter from header value with parameters
 * @return Charset or null if not specified
 */
fun HeaderValueWithParameters.charset(): Charset?

/**
 * Add charset parameter to content type
 * @param charset Charset to add
 * @return New ContentType with charset parameter
 */
fun ContentType.withCharset(charset: Charset): ContentType

/**
 * Add charset parameter to content type only if not already present
 * @param charset Charset to add
 * @return ContentType with charset (original if already present)
 */
fun ContentType.withCharsetIfNeeded(charset: Charset): ContentType

File Extension Mapping

Utilities for mapping file extensions to content types.

/**
 * Get default content type for file extension
 * @param extension File extension (without dot)
 * @return Default ContentType for extension
 */
fun ContentType.Companion.defaultForFileExtension(extension: String): ContentType

/**
 * Get default content type for file path
 * @param path File path  
 * @return Default ContentType based on file extension
 */
fun ContentType.Companion.defaultForFilePath(path: String): ContentType

/**
 * Get possible content types for file extension
 * @param extension File extension (without dot)
 * @return List of possible ContentType instances
 */
fun ContentType.Companion.fromFileExtension(extension: String): List<ContentType>

/**
 * Get possible content types for file path
 * @param path File path
 * @return List of possible ContentType instances
 */
fun ContentType.Companion.fromFilePath(path: String): List<ContentType>

/**
 * Get file extensions for content type
 * @return List of file extensions (without dots)
 */  
fun ContentType.fileExtensions(): List<String>

Exception Types

Exception thrown for malformed content type strings.

/**
 * Exception thrown when content type format is invalid
 */
class BadContentTypeFormatException(message: String) : Exception(message)

Usage Examples:

import io.ktor.http.*
import java.nio.charset.StandardCharsets

// Use predefined content types
val jsonType = ContentType.Application.Json
val htmlType = ContentType.Text.Html
val imageType = ContentType.Image.PNG

// Parse content type from string
val parsed = ContentType.parse("application/json; charset=utf-8")

// Add parameters
val withCharset = ContentType.Application.Json.withCharset(StandardCharsets.UTF_8)
val withCustomParam = ContentType.Text.Plain.withParameter("boundary", "something")

// Content type matching
val matches = ContentType.Application.Json.match(ContentType.Application.Json) // true
val matchesPattern = ContentType.Application.Json.match("application/*") // true

// Check type categories
val isApplication = ContentType.Application.contains(ContentType.Application.Json) // true
val isText = ContentType.Text.contains("text/plain") // true

// File extension mapping
val pdfType = ContentType.defaultForFileExtension("pdf")
val htmlType = ContentType.defaultForFilePath("/path/to/file.html")
val extensions = ContentType.Application.Json.fileExtensions() // ["json"]

// Work with charset
val charset = parsed.charset() // UTF-8 if specified
val withUtf8 = ContentType.Text.Plain.withCharsetIfNeeded(StandardCharsets.UTF_8)

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-http-js

docs

authentication.md

content-types.md

content.md

cookies.md

headers.md

http-core.md

index.md

urls.md

tile.json