I'm trying to communicate with my servers from Lua to authenticate a user. This is what my request function looks like:
function http.send(url)
local req = require("socket.http")
local b, c, h = req.request{
url = url,
redirect = true
}
return b
end
However, I noticed that the data is discarded because I did not provide the sink
parameter. I want to be able to return the downloaded data as a whole string, not download to a file/table. How would I go about this?
You can use ltn12.sink.table
to collect the results into a given table piece by piece. Then you can use table.concat
to get the resulting string.
Example of use from the documentation of ltn12.sink
:
-- load needed modules
local http = require("socket.http")
local ltn12 = require("ltn12")
-- a simplified http.get function
function http.get(u)
local t = {}
local status, code, headers = http.request{
url = u,
sink = ltn12.sink.table(t)
}
return table.concat(t), headers, code
end