Webhook Relay
Search…
Making HTTP Requests
Functions can call 3rd party HTTP servers to get additional information for your webhook or just make an API call and end function execution.
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:
1
-- importing HTTP package
2
local http = require("http")
3
​
4
-- making HTTP call
5
response, err = http.request("GET", "https://example.com")
6
if err then error(err) end
7
​
8
-- parsing response body from the API
9
local api_response, err = json.decode(response.body)
10
if err then error(err) end
Copied!

Specify request body

You can also make a POST, PUT, DELETE requests to a 3rd party APIs:
1
-- importing HTTP and JSON packages
2
local json = require("json")
3
local http = require("http")
4
​
5
local payload, err = json.encode({
6
action = "create_record",
7
user = "example",
8
})
9
if err then error(err) end
10
​
11
local resp, err = http.request("POST", "http://example.com/api", { body = payload})
12
if err then error(err) end
Copied!

Reading response body

To read response body:
1
-- importing HTTP package
2
local http = require("http")
3
​
4
-- making HTTP call
5
response, err = http.request("GET", "https://example.com")
6
if err then error(err) end
7
​
8
-- received JSON string:
9
-- {
10
-- "firstname": "luke",
11
-- "lastname": "skywalker"
12
-- }
13
​
14
-- parsing response body from the API
15
local api_response, err = json.decode(response.body)
16
if err then error(err) end
17
​
18
-- access values like 'api_response.firstname'
Copied!

Query, headers, timeout

To specify query, timeout and headers:
1
-- importing HTTP package
2
local http = require("http")
3
​
4
-- specifying headers
5
local headers = {}
6
headers["Authorization"] = "Basic " .. "123456"
7
​
8
-- making HTTP call
9
response, err = http.request("GET", "https://example.com", {
10
query="page=1",
11
timeout="5s",
12
headers={
13
Accept="*/*"
14
}
15
})
16
if err then error(err) end
17
​
18
-- parsing response body from the API
19
local api_response, err = json.decode(response.body)
20
if err then error(err) end
Copied!

HTTP package API reference

http.delete(url [, options])

Attributes

Name
Type
Description
url
String
URL of the resource to load
options
Table
Additional options

Options

Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
headers
Table
Additional headers to send with the request

Returns

​http.response or (nil, error message)

http.get(url [, options])

Attributes

Name
Type
Description
url
String
URL of the resource to load
options
Table
Additional options

Options

Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
headers
Table
Additional headers to send with the request

Returns

​http.response or (nil, error message)

http.head(url [, options])

Attributes
Name
Type
Description
url
String
URL of the resource to load
options
Table
Additional options
Options
Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
headers
Table
Additional headers to send with the request
Returns
​http.response or (nil, error message)

http.patch(url [, options])

Attributes
Name
Type
Description
url
String
URL of the resource to load
options
Table
Additional options
Options
Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
body
String
Request body.
form
String
Deprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headers
Table
Additional headers to send with the request
Returns
​http.response or (nil, error message)

http.post(url [, options])

Attributes
Name
Type
Description
url
String
URL of the resource to load
options
Table
Additional options
Options
Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
body
String
Request body.
form
String
Deprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headers
Table
Additional headers to send with the request
Returns
​http.response or (nil, error message)

http.put(url [, options])

Attributes
Name
Type
Description
url
String
URL of the resource to load
options
Table
Additional options
Options
Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
body
String
Request body.
form
String
Deprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headers
Table
Additional headers to send with the request
Returns
​http.response or (nil, error message)

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

Attributes

Name
Type
Description
method
String
The HTTP request method
url
String
URL of the resource to load
options
Table
Additional options

Options

Name
Type
Description
query
String
URL encoded query params
cookies
Table
Additional cookies to send with the request
body
String
Request body.
form
String
Deprecated. URL encoded request body. This will also set the Content-Type header to application/x-www-form-urlencoded
headers
Table
Additional 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
Name
Type
Description
body
String
The HTTP response body
body_size
Number
The size of the HTTP response body in bytes
headers
Table
The HTTP response headers
cookies
Table
The cookies sent by the server in the HTTP response
status_code
Number
The HTTP response status code
url
String
The final URL the request ended pointing to after redirects