KtorHttpRequest

class KtorHttpRequest(val builder: HttpRequestBuilder = HttpRequestBuilder()) : HttpRequest

Ktor-based HTTP request implementation.

This class implements the HttpRequest interface using Ktor's HttpRequestBuilder. It provides a Kotlin-idiomatic way to build HTTP requests with support for all common HTTP methods, headers, parameters, cookies, and body formats.

Important Notes

  • Form Accumulation: Unlike typical builders, calling form multiple times accumulates all parameters into a single form body. This is intentional for flexible composition.

  • Default Method: The HTTP method is GET by default. It's automatically changed to POST when calling post or form, PUT when calling put, and DELETE when calling delete.

  • Cookie Format: Cookies must be in Set-Cookie header format (e.g., "name=value; Path=/; HttpOnly")

  • Thread Safety: This class is NOT thread-safe. Create separate instances for concurrent requests.

See also

HttpRequestBuilder

Constructors

Link copied to clipboard
constructor(builder: HttpRequestBuilder = HttpRequestBuilder())

Properties

Link copied to clipboard
@VisibleForTesting
val builder: HttpRequestBuilder

The underlying Ktor HttpRequestBuilder used to construct the request. Exposed as internal for testing and advanced use cases.

Link copied to clipboard
open override var url: String

Gets or sets the URL of the request.

Functions

Link copied to clipboard
open override fun cookie(cookie: String)

Adds a single cookie to the request.

Link copied to clipboard
open override fun cookies(cookies: List<String>)

Adds multiple cookies to the request.

Link copied to clipboard
open override fun delete(body: JsonObject)

Sets the HTTP method to DELETE and optionally sets a JSON body.

open override fun delete(contentType: String, body: String)

Sets the HTTP method to DELETE with custom content type and body.

Link copied to clipboard
open override fun form(formBuilder: MutableMap<String, String>.() -> Unit)

Sets the request body as form data and changes the HTTP method to POST.

Link copied to clipboard
open override fun header(name: String): String?

Retrieves the value(s) of a header by name.

open override fun header(name: String, value: String)

Adds a header to the request.

Link copied to clipboard
open override fun method(): String

Returns the HTTP method of the request as a string.

Link copied to clipboard
open override fun parameter(name: String, value: String)

Adds a query parameter to the request URL.

Link copied to clipboard
open override fun post(body: JsonObject)

Sets the request body as JSON and changes the HTTP method to POST.

open override fun post(contentType: String, body: String)

Sets the request body with POST method and custom content type.

Link copied to clipboard
open override fun put(body: JsonObject)

Sets the HTTP method to PUT and sets a JSON body.

open override fun put(contentType: String, body: String)

Sets the HTTP method to PUT with custom content type and body.