Home

Awesome

gluahttp

gluahttp provides an easy way to make HTTP requests from within GopherLua.

Installation

go get github.com/cjoudrey/gluahttp

Usage

import "github.com/yuin/gopher-lua"
import "github.com/cjoudrey/gluahttp"

func main() {
    L := lua.NewState()
    defer L.Close()

    L.PreloadModule("http", NewHttpModule(&http.Client{}).Loader)

    if err := L.DoString(`

        local http = require("http")

        response, error_message = http.request("GET", "http://example.com", {
            query="page=1",
            timeout="30s",
            headers={
                Accept="*/*"
            }
        })

    `); err != nil {
        panic(err)
    }
}

API

http.delete(url [, options])

Attributes

NameTypeDescription
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.get(url [, options])

Attributes

NameTypeDescription
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.head(url [, options])

Attributes

NameTypeDescription
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.patch(url [, options])

Attributes

NameTypeDescription
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
bodyStringRequest body.
formStringDeprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.post(url [, options])

Attributes

NameTypeDescription
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
bodyStringRequest body.
formStringDeprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.put(url [, options])

Attributes

NameTypeDescription
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
bodyStringRequest body.
formStringDeprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.request(method, url [, options])

Attributes

NameTypeDescription
methodStringThe HTTP request method
urlStringURL of the resource to load
optionsTableAdditional options

Options

NameTypeDescription
queryStringURL encoded query params
cookiesTableAdditional cookies to send with the request
bodyStringRequest body.
formStringDeprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headersTableAdditional headers to send with the request
timeoutNumber/StringRequest timeout. Number of seconds or String such as "1h"
authTableUsername and password for HTTP basic auth. Table keys are user for username, pass for passwod. auth={user="user", pass="pass"}

Returns

http.response or (nil, error message)

http.request_batch(requests)

Attributes

NameTypeDescription
requestsTableA table of requests to send. Each request item is by itself a table containing http.request parameters for the request

Returns

[http.response] or ([http.response], [error message])

http.response

The http.response table contains information about a completed HTTP request.

Attributes

NameTypeDescription
bodyStringThe HTTP response body
body_sizeNumberThe size of the HTTP reponse body in bytes
headersTableThe HTTP response headers
cookiesTableThe cookies sent by the server in the HTTP response
status_codeNumberThe HTTP response status code
urlStringThe final URL the request ended pointing to after redirects