Отправка файла POST-запросом

andrey_hacker

Известный
Автор темы
274
88
Версия MoonLoader
.026-beta
Всем привет, помогите, как сделать отправку файла пост запросом ( для вк ) ? Буду благодарен за помощь
 
1589148804783.png
 

MaksQ

Известный
967
817
Пример
local rc, content = upload_file('http://127.0.0.1:826/api/upload', 'test.png')

Lua:
local https = require 'ssl.https'
local ltn12 = require 'ltn12'
local lfs = require 'lfs'
https.TIMEOUT = 5

function upload_file(url, filename)
    local fileHandle = io.open(filename, 'rb')
    if fileHandle then
        local fileContent = fileHandle:read('*a')
        fileHandle:close()
        local boundary = 'abcd'
        local header_b = 'Content-Disposition: form-data; name="file"; filename="' .. filename .. '"\r\nContent-Type: text/plain\r\n'
        local fileContent =  '--' ..boundary .. '\r\n' ..header_b ..'\r\n'.. fileContent .. '\r\n--' .. boundary ..'--\r\n'
        local response_body = { }
        local _, code = https.request {
            url = url ,
            method = 'POST',
            headers = {
                ['Content-Length'] =  fileContent:len(),
                ['Content-Type'] = 'multipart/form-data; boundary='..boundary
            },
            source = ltn12.source.string(fileContent),
            sink = ltn12.sink.table(response_body),
        }
        return code, table.concat(response_body)
    else
        return false, 'File Not Found'
    end
end
 
  • Грустно
Реакции: moreveal