01.02.2018, 13:09
Try this, I've tested it with nginx and apache so other servers might reject this request. Fill params table with your post arguments, filedata variable should contain binary data of your jpeg image, set request url as needed.
Code:
require('ssl.https')
boundary = os.date('%d%m%Y%H%M%S')
params = {
{
name = 'token',
value = '12345',
},
{
name = 'user',
value = 'testuser',
},
{
name = 'attachment',
filename = 'snapshot.jpg',
ctype = 'image/jpeg',
value = filedata,
}
}
body = { '--' .. boundary }
for _, param in ipairs(params) do
line = string.format('Content-Disposition: form-data; name=%q', param.name)
if param.filename then
line = string.format('%s; filename=%q', line, param.filename)
end
body[ #body + 1 ] = line
if param.ctype then
body[ #body + 1 ] = string.format('Content-Type: %s', param.ctype)
end
body[ #body + 1 ] = ''
body[ #body + 1 ] = param.value
body[ #body + 1 ] = '--' .. boundary
end
-- last boundary
body[ #body ] = body[ #body ] .. '--'
-- empty line at the end
body[ #body + 1 ] = ''
bodydata = table.concat(body, '\r\n')
resp = {}
log(
ssl.https.request({
url = 'https://...',
sink = ltn12.sink.table(resp),
method = 'POST',
source = ltn12.source.string(bodydata),
headers = {
['content-length'] = #bodydata,
['content-type'] = 'multipart/form-data; boundary=' .. boundary
}
})
)
log(table.concat(resp))