HTTP
http itu client HTTP global yang async. Backed by reqwest plus rustls + json + socks — sama plumbing-nya kayak licensing layer, jadi request bisa nembak https + socks5 transparan. Tiap call bakal yield script-nya sampai round-trip selesai (atau timeout-nya kena).
local res = http.get("https://api.example.com/data")
print(res.status, res.ok, res.body)
Opsi request
Tiap method nerima URL plus tabel HttpRequestOpts opsional. Semua field opsional — pass nil/skip aja kalau mau pake default.
| Key | Tipe | Default | Catatan |
|---|---|---|---|
headers | {[string]: string}? | — | Header request custom. Last value wins kalau ada duplicate. |
body | string? | — | Body string mentah. Mutex sama json (kalau dua-duanya di-set, json yang menang). |
json | any? | — | JSON-serialize value-nya, set Content-Type: application/json, kirim sebagai body. |
timeout | number? | 30000 | Timeout per-request dalam milliseconds. |
Shape response
Tiap call http.* balikin tabel HttpResponse. Body-nya selalu dibaca habis sebagai string — ga ada varian streaming.
| Field | Tipe | Catatan |
|---|---|---|
status | number | HTTP status sebagai integer (200, 404, 500, …). |
ok | boolean | true kalau status 2xx, false selain itu. |
body | string | Body response sebagai string UTF-8. Kosong kalau response ga bawa body. |
headers | {[string]: string} | Header response sebagai tabel flat. Last-value-wins kalau ada duplicate. |
Methods
http:get(url, opts?)
| Field | Tipe |
|---|---|
| Signature | (self: Http, url: string, opts: HttpRequestOpts?) → HttpResponse |
| Returns | HttpResponse — lihat shape di atas |
| Common errors | raise kalau URL salah, network gagal, timeout, atau arg json invalid |
| Async | yes — yield sampai response selesai atau timeout-nya kena |
local res = http.get("https://api.example.com/users", {
headers = { ["Authorization"] = "Bearer xxx" },
timeout = 5000,
})
if res.ok then
print(res.body)
end
http:post(url, opts?)
| Field | Tipe |
|---|---|
| Signature | (self: Http, url: string, opts: HttpRequestOpts?) → HttpResponse |
| Returns | HttpResponse — lihat shape di atas |
| Common errors | raise kalau URL salah, network gagal, timeout, atau arg json invalid |
| Async | yes — yield sampai response selesai atau timeout-nya kena |
local res = http.post("https://discord.com/api/webhooks/...", {
json = { content = "bot online" },
headers = { ["Content-Type"] = "application/json" },
timeout = 10000,
})
print(res.status)
http:put(url, opts?)
| Field | Tipe |
|---|---|
| Signature | (self: Http, url: string, opts: HttpRequestOpts?) → HttpResponse |
| Returns | HttpResponse — lihat shape di atas |
| Common errors | raise kalau URL salah, network gagal, timeout, atau arg json invalid |
| Async | yes — yield sampai response selesai atau timeout-nya kena |
http.put("https://api.example.com/users/42", {
json = { name = "renamed" },
})
http:delete(url, opts?)
| Field | Tipe |
|---|---|
| Signature | (self: Http, url: string, opts: HttpRequestOpts?) → HttpResponse |
| Returns | HttpResponse — lihat shape di atas |
| Common errors | raise kalau URL salah, network gagal, timeout, atau arg json invalid |
| Async | yes — yield sampai response selesai atau timeout-nya kena |
http.delete("https://api.example.com/users/42")
Lihat juga
- JSON — pasangkan sama
httpbuat encode/decode body lebih rapi