触摸精灵POST请求网络数据

发布于 2019-05-25 23:39:26

提交数据和请求地址自行修改

function main()
    local http = require "socket.http"
    local ltn12 = require "ltn12"
    local request_body = [[login=user&password=123]] -- 这里是POST的数据
    local response_body = {}
    local res, code, response_headers = http.request{
        url = "http://127.0.0.1:8888", -- 这里是你提交的域名
        method = "POST",
        headers =
        {
            ["Content-Type"] = "application/x-www-form-urlencoded";
            ["Content-Length"] = #request_body;
        },
        source = ltn12.source.string(request_body),
        sink = ltn12.sink.table(response_body),
    }
    logDebug(res)
    logDebug(code)
    if type(response_body) == "table" then
        logDebug(table.concat(response_body))
    else
        logDebug("Not a table:", type(response_body))
    end
end
3 条评论

发布
问题