Skip to main content

//clika-runtime/io.clika.runtime/Http

Http

[common]
object Http

The runtime's HTTP client: text and byte downloads, a download to a file (resumable), the full response of a GET, a POST or a DELETE, and the streaming reads whose chunks or events arrive through a sink on the calling thread. A failure to complete a request is a ClikaRtException; an answered status is data in the HttpResponse.

val page = Http.getText("https://example.com/")
val reply = Http.post("https://api.example.com/v1/chat", """{"prompt": "hi"}""")
if (reply.isOk) println(reply.text())

Functions​

NameSummary
delete[common]
fun delete(url: String, options: DownloadOptions = DEFAULTS): HttpResponse
DELETE url and answer the full response.
downloadBytes[common]
fun downloadBytes(url: String, options: DownloadOptions = DEFAULTS): ByteArray
GET url and answer its body bytes.
downloadFile[common]
fun downloadFile(url: String, destPath: String, options: DownloadOptions = DEFAULTS): String
Download url to destPath and answer the path written; a partial file resumes when DownloadOptions.resume is set.
fetch[common]
fun fetch(url: String, options: DownloadOptions = DEFAULTS): HttpResponse
GET url and answer the full response, any status as data.
fetchFile[common]
fun fetchFile(url: String, destPath: String, options: DownloadOptions = DEFAULTS): HttpResponse
GET url into destPath and answer the response facts (its body streamed to the file).
getSse[common]
fun getSse(url: String, sink: SseSink, options: DownloadOptions = DEFAULTS)
GET url as a Server-Sent-Events stream, each event to sink.
getStream[common]
fun getStream(url: String, sink: ChunkSink, options: DownloadOptions = DEFAULTS)
GET url and hand each body chunk to sink as it arrives; a sink that throws stops the transfer with CANCELLED.
getText[common]
fun getText(url: String, options: DownloadOptions = DEFAULTS): String
GET url and answer its body as text.
post[common]
fun post(url: String, body: String, contentType: String = "application/json", options: DownloadOptions = DEFAULTS): HttpResponse
POST body to url and answer the full response.
postSse[common]
fun postSse(url: String, body: String, contentType: String, sink: SseSink, options: DownloadOptions = DEFAULTS)
POST body to url and read the answer as a Server-Sent-Events stream, each event to sink.
postStream[common]
fun postStream(url: String, body: String, contentType: String, sink: ChunkSink, options: DownloadOptions = DEFAULTS)
POST body to url and hand each response chunk to sink.
postText[common]
fun postText(url: String, body: String, contentType: String = "application/json", options: DownloadOptions = DEFAULTS): String
POST body to url with contentType and answer the response body as text.