Socket – Lua Socket.http Receiver Parameters

I’m trying to communicate with Lua’s server to verify user identity. This is how 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 was discarded because I did not provide a sink parameter. I want to be able to download the data as the whole The string is returned instead of being downloaded to a file/table. What should I do?

You can use ltn12.sink.table to collect the results one by one The given table. Then you can use table.concat to get the result string.

The example used from the ltn12.sink documentation:

-- 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

Me I am trying to communicate with Lua’s server to verify user identity. This is how 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 was discarded because I did not provide a sink parameter. I want to be able to return the downloaded data as a whole string instead of downloading to a file/table. what should I do?

You can use ltn12.sink.table to collect the results into a given table one by one. Then you can use table. concat to get the result string.

The example used from the ltn12.sink document:

-- 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

Leave a Comment

Your email address will not be published.