Search code examples
socketshttpdownloadlualuasocket

Lua HTTP download and save to file


I'm trying to download and then save the contents to a xml file. The code I got is:

local filePath = currentDir().."/file.xml"

local http  = require("socket.http")
local xFile = io.open(filePath, "w")
local save = ltn12.sink.file(xFile)

http.request{addr, sink = save }
print("Done!")

It runs but the file is still empty. Can I get some help here please?


Solution

  • Its a syntactical mistake. You mixed two styles of calling http.request. Use

    http.request{url = addr, sink = save }