net
Built-in library for network access
Example usage
Functions
jsonDecode
Decodes the given JSON string into a lua value.
jsonEncode
Encodes the given value as JSON.
request
Sends an HTTP request using the given url and / or parameters, and returns a dictionary that describes the response received.
Only throws an error if a miscellaneous network or I/O error occurs, never for unsuccessful status codes.
serve
Creates an HTTP server that listens on the given port
.
This will not block and will keep listening for requests on the given port
until the stop
function on the returned ServeHandle
has been called.
socket
Connects to a web socket at the given URL.
Throws an error if the server at the given URL does not support web sockets, or if a miscellaneous network or I/O error occurs.
urlDecode
Decodes the given string using URL decoding.
urlEncode
Encodes the given string using URL encoding.
Types
FetchParams
Parameters for sending network requests with net.request
.
This is a dictionary that may contain one or more of the following values:
url
- The URL to send a request to. This is always requiredmethod
- The HTTP method verb, such as"GET"
,"POST"
,"PATCH"
,"PUT"
, or"DELETE"
. Defaults to"GET"
query
- A table of key-value pairs representing query parameters in the request pathheaders
- A table of key-value pairs representing headersbody
- The request body
FetchResponse
Response type for sending network requests with net.request
.
This is a dictionary containing the following values:
ok
- If the status code is a canonical success status code, meaning within the range 200 -> 299statusCode
- The status code returned for the requeststatusMessage
- The canonical status message for the returned status code, such as"Not Found"
for status code 404headers
- A table of key-value pairs representing headersbody
- The request body, or an empty string if one was not given
HttpMethod
ServeConfig
Configuration for net.serve
.
This may contain one of, or both of the following callbacks:
handleRequest
for handling normal http requests, equivalent to just passing a function tonet.serve
handleWebSocket
for handling web socket requests, which will receive aWebSocket
object as its first and only parameter
ServeHandle
A handle to a currently running web server, containing a single stop
function to gracefully shut down the web server.
ServeHttpHandler
ServeRequest
Data type for requests in net.serve
.
This is a dictionary containing the following values:
path
- The path being requested, relative to the root. Will be/
if not specifiedquery
- A table of key-value pairs representing query parameters in the request pathmethod
- The HTTP method verb, such as"GET"
,"POST"
,"PATCH"
,"PUT"
, or"DELETE"
. Will always be uppercaseheaders
- A table of key-value pairs representing headersbody
- The request body, or an empty string if one was not given
ServeResponse
Response type for requests in net.serve
.
This is a dictionary that may contain one or more of the following values:
status
- The status code for the request, in the range100
->599
headers
- A table of key-value pairs representing headersbody
- The response body
ServeWebSocketHandler
WebSocket
A reference to a web socket connection.
The web socket may be in either an "open" or a "closed" state, changing its current behavior.
When open:
Any function on the socket such as
send
,next
orclose
can be called without erroringnext
can be called to yield until the next message is received or the socket becomes closed
When closed:
next
will no longer return any message(s) and instead instantly return nilsend
will throw an error stating that the socket has been closed
Once the websocket has been closed, closeCode
will no longer be nil, and will be populated with a close code according to the WebSocket specification. This will be an integer between 1000 and 4999, where 1000 is the canonical code for normal, error-free closure.
Last updated