Skip to content

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) -> table

Request Options

KeyTypeDefaultDescription
Urlstring(Required) The URL to request
Methodstring"GET"HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD)
Headerstable{}Request headers as key-value pairs
Cookiestable{}Cookies as key-value pairs
Bodystring?nilRequest body (required for POST/PUT/PATCH)

Response Table

KeyTypeDescription
SuccessbooleanWhether the request was successful (2xx status)
StatusCodenumberHTTP status code
StatusMessagestringHTTP status message
HeaderstableResponse headers
CookiestableResponse cookies
BodystringResponse 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
ParameterTypeDescription
urlstringThe WebSocket server URL (ws:// or wss://)

WebSocketClient

MemberTypeDescription
Send(message)methodSends a message to the server
Close()methodCloses the connection
OnMessageSignalFired when a message is received
OnCloseSignalFired 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()