Network
Functions for making HTTP requests and establishing WebSocket connections.
request
Makes a RESTful HTTP request and returns the response.
Aliases: httprequest, http_request, http.request
lua
request(options: table) -> tableRequest Options
| Key | Type | Default | Description |
|---|---|---|---|
Url | string | — | (Required) The URL to request |
Method | string | "GET" | HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD) |
Headers | table | {} | Request headers as key-value pairs |
Cookies | table | {} | Cookies as key-value pairs |
Body | string? | nil | Request body (required for POST/PUT/PATCH) |
Response Table
| Key | Type | Description |
|---|---|---|
Success | boolean | Whether the request was successful (2xx status) |
StatusCode | number | HTTP status code |
StatusMessage | string | HTTP status message |
Headers | table | Response headers |
Cookies | table | Response cookies |
Body | string | Response body |
Example
lua
-- GET request
local response = request({
Url = "https://httpbin.org/get",
Method = "GET"
})
print(response.StatusCode) --> 200
print(response.Body)
-- POST request with JSON body
local response = request({
Url = "https://httpbin.org/post",
Method = "POST",
Headers = {
["Content-Type"] = "application/json"
},
Body = '{"key": "value"}'
})
print(response.Body)WebSocket
The WebSocket library allows you to create real-time WebSocket connections.
WebSocket.connect
Creates and connects a WebSocket client to the specified URL.
Aliases: wsconnect
lua
WebSocket.connect(url: string) -> WebSocketClient| Parameter | Type | Description |
|---|---|---|
url | string | The WebSocket server URL (ws:// or wss://) |
WebSocketClient
| Member | Type | Description |
|---|---|---|
Send(message) | method | Sends a message to the server |
Close() | method | Closes the connection |
OnMessage | Signal | Fired when a message is received |
OnClose | Signal | Fired when the connection closes |
Example
lua
local ws = WebSocket.connect("wss://echo.websocket.org")
ws.OnMessage:Connect(function(message)
print("Received:", message)
end)
ws.OnClose:Connect(function()
print("Connection closed")
end)
ws:Send("Hello, WebSocket!")
task.wait(2)
ws:Close()