Creating and using a Lua module

This scenario illustrates how to create and then use a Lua module. The scenario involves two Lua scripts, the first which creates the module, and the second which shows how to use the created module. Both scripts should be added to the standard HTTP transformation rule’s location. The location which stores the HTTP transformation rules has been added to the Lua search path. So each Lua script contained in the HTTP transformation rules directory can be located by any other Lua script.

Module Script

-- IBM Confidential 
-- PID 5725-V89 5725-V90 5737-F02
-- 
-- Copyright IBM Corp. 2022, 2022


-- This script is used to test the ability to create a Lua module.  It defines
-- the AddHeaderModule which contains a single function to add a HTTP header to
-- the response.

local AddHeaderModule = {}

function AddHeaderModule.add_header(name, value)
    HTTPResponse.setHeader(name, value)
end

return AddHeaderModule

Transformation Script

-- IBM Confidential 
-- PID 5725-V89 5725-V90 5737-F02
-- 
-- Copyright IBM Corp. 2022, 2022

-- This script is used to test the ability to use a locally created Lua module.
-- It will 'require' the AddHeaderModule module, and use this module to add the
-- specified header to the response.

local module = require "AddHeaderModule"

module.add_header("test-rsp-header", "header-value")