I'm trying to upload an image using luaSocket.
Here is my Lua code:
function uploadFile(dir)
local resp = {}
local body,code,headers,status = http.request{
url = "my_url",
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Content-Length"] = file_size
},
source = ltn12.source.file(io.open(dir),"rb"),
sink = ltn12.sink.table(resp)
}
print(body,code,status)
if headers then for k,v in pairs(headers) do print(k,v) end end end
My php code is:
<?php
copy("php://input","test");
echo("OK");
?>
When I try to upload the image I don't get any error but body and status are nil, but code is "timeout". But the script works fine if I try to upload a text file.
Any help is appreciated. Thanks.
You are passing "rb"
as parameter to ltn12.sink.file
instead of io.open
. Change the statement to:
source = ltn12.source.file( io.open(dir,"rb") ),