Skip to main content

core.http

Coroutine-based HTTP client for Pragtical Code Editor. Supports streaming responses line-by-line or binary, chunked transfer encoding, Content-Length, connection-close streams, automatic redirects, proper port handling, global user agent, and file downloads.

local http = require "core.http"

user_agent

(field) user_agent: string

Default user agent used on all requests.


http.download.options

directory

(field) directory: string?

Path to save the downloaded file


filename

(field) filename: string?

headers

(field) headers: table<string, string>?

is_cancelled

(field) is_cancelled: (fun():boolean)?

on_done

(field) on_done: fun(ok?: boolean, err?: string, filename?: string, info?: http.response_info)

on_progress

(field) on_progress: fun(downloaded: integer, total?: integer)?

timeout

(field) timeout: number?

http.header_value

http.method

http.method:
| "GET"
| "POST"
| "PUT"
| "DELETE"
| "PATCH"
| "HEAD"
| "OPTIONS"

http.on_chunk

http.on_done

http.on_done_download

http.on_header

http.on_progress

http.on_sse_done

http.on_sse_event

http.request.fileparam

content_type

(field) content_type: string

data

(field) data: string?

filename

(field) filename: string

path

(field) path: string?

http.request.options

body

(field) body: string?

decode_json

(field) decode_json: boolean?

(default: true)


headers

(field) headers: table<string, string>?

is_cancelled

(field) is_cancelled: (fun():boolean)?

on_chunk

(field) on_chunk: fun(chunk: string)?

on_done

(field) on_done: fun(ok?: boolean, err?: string, result: string|table|nil, info?: http.response_info)

on_header

(field) on_header: fun(info: http.response_info)?

on_redirect

(field) on_redirect: fun(info: http.response_info)?

redirect_count

(field) redirect_count: integer?

timeout

(field) timeout: number?

http.request.param

http.request.submittype

http.request.submittype:
| "application/x-www-form-urlencoded"
| "multipart/form-data"
| "application/json"
| "text/plain"

http.response_info

headers

(field) headers: table<string, string|string[]>

status

(field) status: integer

url

(field) url: string

http.sse.event

data

(field) data: string

event

(field) event: string

id

(field) id: string?

retry

(field) retry: integer?

http.sse.options

body

(field) body: string?

headers

(field) headers: table<string, string>?

is_cancelled

(field) is_cancelled: (fun():boolean)?

last_event_id

(field) last_event_id: string?

method

(field) method: ("DELETE"|"GET"|"HEAD"|"OPTIONS"|"PATCH"...(+2))?

on_done

(field) on_done: fun(ok?: boolean, err?: string, info?: http.response_info)

on_event

(field) on_event: fun(event: http.sse.event, info?: http.response_info)

on_header

(field) on_header: fun(info: http.response_info)?

on_redirect

(field) on_redirect: fun(info: http.response_info)?

params

(field) params: table<string, string|http.request.fileparam>?

submit_type

(field) submit_type: ("application/json"|"application/x-www-form-urlencoded"|"multipart/form-data"|"text/plain")?

timeout

(field) timeout: number?

delete

function core.http.delete(url: string, submit_type: "application/json"|"application/x-www-form-urlencoded"|"multipart/form-data"|"text/plain", params?: table<string, string>, options: http.request.options)

HTTP DELETE

submit_type:
| "application/x-www-form-urlencoded"
| "multipart/form-data"
| "application/json"
| "text/plain"

download

function core.http.download(url: string, options: http.download.options)

Download a file asynchronously with optional progress tracking.

@param url — File URL


get

function core.http.get(url: string, params?: table<string, string>, options: http.request.options)

HTTP GET


function core.http.head(url: string, params?: table<string, string>, options: http.request.options)

HTTP HEAD


options

function core.http.options(url: string, params?: table<string, string>, options: http.request.options)

HTTP OPTIONS


patch

function core.http.patch(url: string, submit_type: "application/json"|"application/x-www-form-urlencoded"|"multipart/form-data"|"text/plain", params?: table<string, string>, options: http.request.options)

HTTP PATCH

submit_type:
| "application/x-www-form-urlencoded"
| "multipart/form-data"
| "application/json"
| "text/plain"

post

function core.http.post(url: string, submit_type: "application/json"|"application/x-www-form-urlencoded"|"multipart/form-data"|"text/plain", params?: table<string, string>, options: http.request.options)

HTTP POST

submit_type:
| "application/x-www-form-urlencoded"
| "multipart/form-data"
| "application/json"
| "text/plain"

put

function core.http.put(url: string, submit_type: "application/json"|"application/x-www-form-urlencoded"|"multipart/form-data"|"text/plain", params?: table<string, string>, options: http.request.options)

HTTP PUT

submit_type:
| "application/x-www-form-urlencoded"
| "multipart/form-data"
| "application/json"
| "text/plain"

request

function core.http.request(method: "DELETE"|"GET"|"HEAD"|"OPTIONS"|"PATCH"...(+2), url: string, options: http.request.options)

Perform an HTTP request asynchronously.

method:
| "GET"
| "POST"
| "PUT"
| "DELETE"
| "PATCH"
| "HEAD"
| "OPTIONS"

sse

function core.http.sse(url: string, options: http.sse.options)

Open a Server-Sent Events stream. Supports normal SSE GET requests as well as POST-based SSE APIs.