Edit request/response
In this page we will demonstrate basic operations that you can achieve in functions.
Accessing request data (incoming webhook/API request)
Lua functions use r
object that provide access to request data (headers, query, method and body) and can then update any HTTP request details.
Available data to access:
Method name
Type
Description
r.RequestBody
String
Request body.
r.RequestMethod
String
Request method (PUT, POST, DELETE, etc.).
r.RequestPath
String
Request path.
r.RequestRawQuery
String
Request query, for example if the request was made with /api?category=electronics, then the query will be category=electronics.
r.RequestHeader
Table
A table [foo]bar of headers.
r.RequestQuery
Table
A table [foo]bar of query.
Read request body
An example of accessing request body and decoding it:
Reading request headers
To access specific header, use:
Reading request URL query
To read request URL query (for example /v1/api?hub.mode=subscribe&hub.challenge=1903260781&hub.verify_token=my-token"
) you have two options:
r.RequestQuery["hub.challenge"]
which will return1903260781
for this example.r.RequestRawQuery
which will return full raw queryhub.mode=subscribe&hub.challenge=1903260781&hub.verify_token=my-token"
Modify request data
Available methods to update request:
Method name
Parameter Type
Description
r:SetRequestBody(βstringβ)
String
Update request body
r:SetRequestMethod(βstringβ)
String
Update request method
r:SetRequestRawQuery(βfoo=barβ)
String
Update request raw query
r:SetRequestPath(β/extra/pathβ)
String
Set additional extra path
r:DeleteRequestHeader("key")
String
Delete header
r:SetRequestHeader(βkeyβ, βvalueβ)
String, String
Set new header key/value pair
An example how to update request object:
Modify response
Note: customized responses only applicable if function is attached to the Input and not bucketβs Output.
Available methods to set customized response:
Method name
Parameter Type
Description
r:SetResponseBody(βstringβ)
String
Set response body
r:SetResponseStatusCode(201)
Integer
Set response status code
r:SetResponseHeader(βkeyβ, βvalueβ)
String, String
Set response header key/value pair
Getting configuration values
Configuration values in Lua functions allow sharing the code without sharing sensitive information or just configuration values that might change between accounts, teams, etc.
To add a new configuration value:
Create a function
Go to function details
Click on a tab βCONFIG VARIABLESβ
Specify config variable key and value.
Once you have specified these details, you can start using them in your functions:
Filtering requests
To filter requests based on body, headers or some external conditions, you can use r:StopForwarding()
method. This will set webhook status to rejected and this webhook will not be forwarded:
Last updated