I'd like to make a plugin for Family Historian which uses the Genealogie Online Publication API. This basically is a HTTP request (POST) with file upload. But I can't get the file upload to work properly.
Here's the code:
Code: Select all
require 'luacom'
local http = luacom.CreateObject("winhttp.winhttprequest.5.1")
local url = 'https://api.coret.org/publish/create/v1/'
local api_key='***'
http:Open("POST",url,false)
boundary='----------goapipublishcreate'
http:SetRequestHeader("Content-Type", "multipart/form-data; boundary="..boundary)
parameters = ''
parameters = parameters..'--'..boundary..'\r\nContent-Disposition: form-data; name="api_key"\r\n\r\n'..api_key..'\r\n'
parameters = parameters..'--'..boundary..'\r\nContent-Disposition: form-data; name="bestand_gedcom_zip"; filename="test.zip"\r\nContent-Type: application/x-zip-compressed\r\n\r\n'
local fh = assert(io.open('c:\\temp\\test.zip', "rb"))
local contents = assert(fh:read("*a"))
fh:close()
parameters = parameters..contents..'\r\n'
parameters = parameters..'--'..boundary..'--'
http:SetRequestHeader("Content-Length", string.len(parameters))
http:SetRequestHeader("Connection", "close")
print(string.len(parameters))
http:Send(parameters)
http:WaitForResponse(30)
I've printed the length of the parameters variable (which includes the binary contents of test.zip) and from that I can conclude that the parameters variable does contains the complete file. But when call http:Send only the part of the parameters variable upon the (first) zero byte is processed.
Any LUA experts here who can suggest solution?
Any alternative modules available to file uploading in stead of luacom.CreateObject("winhttp.winhttprequest.5.1")?
Regards,
Bob