HTTP Networking (`request`)

Perform asynchronous and synchronous HTTP/HTTPS requests, consume REST APIs, send JSON payloads, and inspect status codes directly from PyLua.

1. Getting Started

import_request.lua
import request

response = request.get("https://api.github.com/repos/sabbir28/PyLua")
if (response.status == 200) {
    print("Response Body: " .. response.body)
} else {
    print("HTTP Request failed with status code: " .. response.status)
}

2. Methods Reference

Function Signature Description
request.get(url, [options]) (string, [table]) → Response Executes an HTTP GET request to the target URL.
request.post(url, body, [options]) (string, string, [table]) → Response Executes an HTTP POST request sending body as payload.

Response Object Properties

  • res.status: Integer HTTP status code (e.g. 200, 201, 404, 500).
  • res.body: String containing the raw response body.
  • res.headers: Key-value table of received HTTP response headers.

3. Sending Custom Headers & JSON Post

post_json.lua
import request

payload = '{"player": "Aria", "score": 9500}'
options = {
    headers = {
        ["Content-Type"] = "application/json",
        ["Authorization"] = "Bearer secret_api_token"
    }
}

res = request.post("https://example.com/api/leaderboard", payload, options)

if (res.status == 201 or res.status == 200) {
    print("Successfully submitted high score!")
}