4

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?

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.