[SOLVED] Parameter in callback of http.request() function

On API reference (http) you can see the following example

local function http_result(self, _, response)
    print(response.status)
    print(response.response)
    pprint(response.headers)
end

function init(self)
    http.request("http://www.google.com", "GET", http_result)
end

How can I add a parameter to the callback function? When I try it I get an error. Something like this:

function init(self)
    http.request("http://www.google.com", "GET", http_result(MY_PARAMETER)
end

You can achieve this in a few ways. One option is this:


function init(self)
    local foobar = 1234

    local function http_result(self, _, response)
        print(foobar)           -- 1234
        print(response.status)
        print(response.response)
        pprint(response.headers)
    end

    http.request("http://www.google.com", "GET", http_result)
end

Or as an anonymous function:

function init(self)
    local foobar = 1234

    http.request("http://www.google.com", "GET", function(self, _, response)
        print(foobar)           -- 1234
        print(response.status)
        print(response.response)
        pprint(response.headers)
    end)
end
4 Likes