DocumentationFundamentals

Make HTTP request

Making HTTP requests from Webhook Relay Functions

How to make an HTTP call from within the function

Functions can make multiple HTTP request calls to any external server. Some of the uses cases:

  • Call 3rd party API to get additional authentication tokens before forwarding request.
  • Send data to the external service directly, without relying on the webhook.
  • Get additional data to the function so it can dynamically mutate the payload.

Using HTTP package

To make an HTTP request from Lua function, import 'http' package:

-- importing HTTP package
local http = require("http")

-- making HTTP call
response, err = http.request("GET", "https://example.com")
if err then error(err) end

-- parsing response body from the API
local api_response, err = json.decode(response.body)
if err then error(err) end

Specify request body

You can also make a POST, PUT, DELETE requests to a 3rd party APIs:

-- importing HTTP and JSON packages
local json = require("json")
local http = require("http")

local payload, err = json.encode({
    action = "create_record",
    user = "example",
})
if err then error(err) end

local resp, err = http.request("POST", "http://example.com/api", { body = payload})
if err then error(err) end

Reading response body

To read response body:

-- importing HTTP package
local http = require("http")

-- making HTTP call
response, err = http.request("GET", "https://example.com")
if err then error(err) end

-- received JSON string:
-- {
--   "firstname": "luke",
--   "lastname": "skywalker"  
-- }

-- parsing response body from the API
local api_response, err = json.decode(response.body)
if err then error(err) end

-- access values like 'api_response.firstname'

Query, headers, timeout

To specify query, timeout and headers:

-- importing HTTP package
local http = require("http")

-- specifying headers
local headers = {}
headers["Authorization"] = "Basic " .. "123456"

-- making HTTP call
response, err = http.request("GET", "https://example.com", {
    query="page=1",
    timeout="5s",
    headers={
        Accept="*/*"
    }
})
if err then error(err) end

-- parsing response body from the API
local api_response, err = json.decode(response.body)
if err then error(err) end

HTTP package API reference

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

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

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

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

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

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

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

Returns

http.response or (nil, 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
Did this page help you?