1

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.

bizzobaz
  • 95
  • 1
  • 8

1 Answers1

2

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") ),
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • That's interesting... while it is technically wrong, io.open(dir) *should* still return a proper file handle, and "rb" should be ignored unless the handle is nil. Maybe it's because it's a binary file under windows? – DarkWiiPlayer Aug 02 '18 at 04:53