openresty 学习笔记二:获取请求数据

openresty 获取POST或者GET的请求参数。这个是要用openresty 做接口必须要做的事情。
这里分几种类型:GET,POST(urlencoded),POST(form-data)。可以根据需要选择某种提交请求的方式,也可以集合封装成一个工具库来使用

GET 请求

GET的请求数据比较简单

function _M.get(self)
    local  getArgs = {}
    getArgs = ngx.req.get_uri_args()
    return getArgs
end

POST(urlencoded) 请求

urlencoded类型的POST请求数据也比较简单

function _M.post_urlencoded(self)
    local  postArgs = {}
    postArgs = ngx.req.get_post_args()
    return postArgs
end

POST(form-data) 请求

form-data类型的POST请求数据就比较复杂,需要进行字符串分割(lua好像不带split方法),所以首先要写一个split方法

function _M.split(self,s, delim)

    if type(delim) ~= "string" or string.len(delim) <= 0 then
        return nil
    end
 
    local start = 1
    local t = {}

    while true do
        local pos = string.find (s, delim, start, true) -- plain find
        
        if not pos then
            break
        end
 
        table.insert (t, string.sub (s, start, pos - 1))
        start = pos + string.len (delim)
    end

    table.insert (t, string.sub (s, start))
 
    return t
end

获取form-data的请求参数需要用到upload库来获取表单,这个库安装openresty默认已经带了,也可以上GITHUB下载最新版本

local upload = require "resty.upload"
local form, err = upload:new(4096)

function _M.post_form_data(self,form,err)

    if not form then
        ngx.log(ngx.ERR, "failed to new upload: ", err)
        return {}
    end

    form:set_timeout(1000) -- 1 sec
    local paramTable = {["s"]=1}
    local tempkey = ""
    while true do
        local typ, res, err = form:read()
        if not typ then
            ngx.log(ngx.ERR, "failed to read: ", err)
            return {}
        end
        local key = ""
        local value = ""
        if typ == "header" then
            local key_res = _M:split(res[2],";")
            key_res = key_res[2]
            key_res = _M:split(key_res,"=")
            key = (string.gsub(key_res[2],"\"",""))
            paramTable[key] = ""
            tempkey = key
        end
        if typ == "body" then
            value = res
            if paramTable.s ~= nil then paramTable.s = nil end
            paramTable[tempkey] = value
        end
        if typ == "eof" then
            break
        end
    end
    return paramTable
end

args = _M:post_form_data(form, err)

根据请求类型不同使用不同方法进行获取

根据需要,也可以将其合并起来

function _M.new()
    local args = {}
    local requestMethod = ngx.var.request_method
    local receiveHeaders = ngx.req.get_headers()
    local upload = require "resty.upload"
    local form, err = upload:new(chunk_size)

    if "GET" == requestMethod then
        args = _M:get()
    elseif "POST" == requestMethod then
        ngx.req.read_body()
        if string.sub(receiveHeaders["content-type"],1,20) == "multipart/form-data;" then
            args = _M:post_form_data(form, err)
        else
            args = _M:post_urlencoded()
        end
    end

    return args
end

return _M

仅有一条评论

  1. 朱欣磊 朱欣磊

    您好,我是一名phper,刚学习openresty不久,麻烦问您一个问题
    请问,客户端如何通过form表单发送post请求交给lua处理呢?难道要向php那样在form表单的action中指定要处理的php文件那样指定相应的lua文件吗?

添加新评论