Skip to content
Untitled Sandbox - Developer Documentation
docs/api/http

@usbx/http

Web requests and JSON

local http = require("@usbx/http")

http.request

functionwaits

Sends a request and yields until the response is back. A request that fails, or goes over 300 requests a minute, gives ok = false, status 0 and the error.

http.request(options: HttpRequest) -> HttpResponse
example
local response = http.request({ url = "https://example.com" })

Use it forfetching data from a web service, or posting scores to your own website.

optionsThe url, and the method, headers and body if needed.
Build a request callurl · method · headers · body · timeout
  • urlrequired

    The http or https address to call, such as https://example.com/scores, up to 2048 characters.

  • method

    The kind of request to make, which is GET when you leave it out.

  • headers

    A table of header names and their values, with at most 32 headers.

  • body

    The text to send, up to 1 MB, which GET and HEAD requests never send.

  • timeout

    How many seconds to wait, which is 10 by default and at most 30.

http.json_encode

function

JSON text of a value. A table with only the keys 1 to n is an array.

http.json_encode(value: any) -> string
example
local text = http.json_encode({ score = 10 })

Use it forturning a table into text so you can send it to a web service.

valueA table, string, number, boolean or nil.

http.json_decode

function

The value of JSON text. null is nil.

http.json_decode(text: string) -> any
example
local value = http.json_decode('{"score": 10}')

Use it forreading a web service's reply, which arrives as text, back into a table you can use.

textJSON text.

http.url_encode

function

Percent-encodes text for use in a URL. Letters, digits, -, _, ~ and the dot stay as they are.

http.url_encode(text: string) -> string
example
local query = http.url_encode("hello world")

Use it forputting a player's search text into a web address without spaces or symbols breaking it.

textThe text to encode.
↑ ↓ moveEnter openEsc close