local copas = require 'copas'
local http = require 'copas.http'
-- Enhanced httpRequest function with cache-control headers
function httpRequest(request, body, handler)
local function executeRequest(url, requestBody, responseHandler)
local headers = {
["Cache-Control"] = "no-cache", -- Prevent caching
["Pragma"] = "no-cache", -- Legacy directive for HTTP/1.0
["Expires"] = "0" -- Expired immediately
}
local options = {
url = url,
headers = headers,
method = "GET" -- Use "GET" or "POST" based on request type
}
local response, status, headers = http.request(options)
-- Handle the response using the provided handler function
if responseHandler then
responseHandler(response, status, headers)
end
end
if not copas.running then
copas.running = true
lua_thread.create(function()
wait(0)
while not copas.finished() do
local ok, err = copas.step(0)
if ok == nil then error(err) end
wait(0)
end
copas.running = false
end)
end
if handler then
return copas.addthread(function(r, b, h)
copas.setErrorHandler(function(err) h(nil, err) end)
executeRequest(r, b, h)
end, request, body, handler)
else
local results
local thread = copas.addthread(function(r, b)
copas.setErrorHandler(function(err) results = {nil, err} end)
results = table.pack(executeRequest(r, b))
end, request, body)
while coroutine.status(thread) ~= 'dead' do wait(0) end
return table.unpack(results)
end
end