Modifying a response

This scenario illustrates how to modify the body of a HTTP response.

Script

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

-- This script is used to test the modification of a JSON based response.  It
-- will change the 'modified' field in the JSON payload from false to
-- true.

local cjson = require "cjson"

-- Only modify the response if the content-type is 'application/json'
if HTTPResponse.getHeader("content-type") == "application/json" then
    local payload = cjson.decode(HTTPResponse.getBody())

    payload["modified"] = true

    HTTPResponse.setBody(cjson.encode(payload))
end