http

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 5 Imported by: 0

README

go-lua-http

A GopherLua module for making HTTP requests from Lua scripts.

Installation

go get github.com/kerimovok/go-lua-http

Usage

In Go
import (
    lua "github.com/yuin/gopher-lua"
    http "github.com/kerimovok/go-lua-http"
)

L := lua.NewState()
defer L.Close()

// Preload the http module
L.PreloadModule("http", http.Loader)

// Now Lua scripts can use require("http")
L.DoString(`
    local http = require("http")
    local resp = http.get("https://api.example.com/data")
    print(resp.status) -- 200
    print(resp.body)
`)
In Lua
local http = require("http")

-- GET request
local resp, err = http.get("https://api.example.com/users/1")
if err then
    error("Request failed: " .. err)
end
print("Status:", resp.status)
print("Body:", resp.body)

-- POST request with headers
local headers = {
    ["Authorization"] = "Bearer token123",
    ["Content-Type"] = "application/json"
}
local resp, err = http.post(
    "https://api.example.com/users",
    '{"name":"John","email":"john@example.com"}',
    headers
)

-- PUT request
local resp, err = http.put(
    "https://api.example.com/users/1",
    '{"name":"John Doe"}',
    {}
)

-- DELETE request
local resp, err = http.delete("https://api.example.com/users/1", {})

Functions

http.get(url, headers?)

Performs a GET request.

  • Parameters:
    • url (string): The URL to request
    • headers (table, optional): Headers as key-value pairs
  • Returns:
    • table: Response table with status, status_text, body, and headers
    • string (error): Error message if request fails
http.post(url, body, headers?)

Performs a POST request.

  • Parameters:
    • url (string): The URL to request
    • body (string): Request body
    • headers (table, optional): Headers as key-value pairs
  • Returns:
    • table: Response table
    • string (error): Error message if request fails
http.put(url, body, headers?)

Performs a PUT request.

  • Parameters:
    • url (string): The URL to request
    • body (string): Request body
    • headers (table, optional): Headers as key-value pairs
  • Returns:
    • table: Response table
    • string (error): Error message if request fails
http.delete(url, headers?)

Performs a DELETE request.

  • Parameters:
    • url (string): The URL to request
    • headers (table, optional): Headers as key-value pairs
  • Returns:
    • table: Response table
    • string (error): Error message if request fails

Response Table

All HTTP functions return a response table with the following fields:

  • status (number): HTTP status code (e.g., 200, 404, 500)
  • status_text (string): HTTP status text (e.g., "200 OK")
  • body (string): Response body as string
  • headers (table): Response headers as key-value pairs

Notes

  • Default timeout is 30 seconds
  • If Content-Type header is not provided and body exists, it defaults to application/json
  • All requests are synchronous and will block until completion or timeout

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Loader

func Loader(L *lua.LState) int

Loader loads the http module

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL