Nginx服务器中使用lua获取get或post参数
2017-04-25 14:14
786 查看
--Nginx服务器中使用lua获取get或post参数 local request_method = ngx.var.request_method local args = nil local param = nil local param2 = nil --获取参数的值 if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() args = ngx.req.get_post_args() end param = args["param"] param2 = args["param2"] --升级版(能处理content-type=multipart/form-data的表单): local function explode ( _str,seperator ) local pos, arr = 0, {} for st, sp in function() return string.find( _str, seperator, pos, true ) end do table.insert( arr, string.sub( _str, pos, st-1 ) ) pos = sp + 1 end table.insert( arr, string.sub( _str, pos ) ) return arr end local args = {} local file_args = {} local is_have_file_param = false local function init_form_args() local receive_headers = ngx.req.get_headers() local request_method = ngx.var.request_method if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() --判断是否是multipart/form-data类型的表单 if string.sub(receive_headers["content-type"],1,20) == "multipart/form-data;" then is_have_file_param = true content_type = receive_headers["content-type"] --body_data可是符合http协议的请求体,不是普通的字符串 body_data = ngx.req.get_body_data() --请求体的size大于nginx配置里的client_body_buffer_size,则会导致请求体被缓冲到磁盘临时文件里,client_body_buffer_size默认是8k或者16k if not body_data then local datafile = ngx.req.get_body_file() if not datafile then error_code = 1 error_msg = "no request body found" else local fh, err = io.open(datafile, "r") if not fh then error_code = 2 error_msg = "failed to open " .. tostring(datafile) .. "for reading: " .. tostring(err) else fh:seek("set") body_data = fh:read("*a") fh:close() if body_data == "" then error_code = 3 error_msg = "request body is empty" end end end end local new_body_data = {} --确保取到请求体的数据 if not error_code then local boundary = "--" .. string.sub(receive_headers["content-type"],31) local body_data_table = explode(tostring(body_data),boundary) local first_string = table.remove(body_data_table,1) local last_string = table.remove(body_data_table) for i,v in ipairs(body_data_table) do local start_pos,end_pos,capture,capture2 = string.find(v,'Content%-Disposition: form%-data; name="(.+)"; filename="(.*)"') --普通参数 if not start_pos then local t = explode(v,"rnrn") local temp_param_name = string.sub(t[1],41,-2) local temp_param_value = string.sub(t[2],1,-3) args[temp_param_name] = temp_param_value else --文件类型的参数,capture是参数名称,capture2是文件名 file_args[capture] = capture2 table.insert(new_body_data,v) end end table.insert(new_body_data,1,first_s e430 tring) table.insert(new_body_data,last_string) --去掉app_key,app_secret等几个参数,把业务级别的参数传给内部的API body_data = table.concat(new_body_data,boundary)--body_data可是符合http协议的请求体,不是普通的字符串 end else args = ngx.req.get_post_args() end end end
相关文章推荐
- lua获取get或post参数
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- 在Express中使用Multiparty进行文件上传及POST、GET参数获取
- ajax使用post提交参数java后台获取值为null但是get却可以获取的问题
- HttpLuaModule 获取Get和Post参数
- Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- 表单中多个请求参数名字相同,服务器到底获取的是哪个请求参数的值,表单的action和get和post提交方式的关系以及提交数据的不同点
- lua在web开发中获取GET或POST参数
- lua在web开发中获取GET或POST参数
- 在ASP.NET中获取参数POST和GET方式提交的参数
- android客户端采用Post和Get方式向web服务器传递参数
- 在ASP.NET中获取参数POST和GET方式提交的参数 .
- 利用Uri获取返回的参数即getQueryParameter()方法的使用
- windows phone 7 通过Post提交URL到服务器,从服务器获取数据(比如登陆时候使用)
- php 获取远程服务器信息 get_headers 的使用
- 使用Ajax的Get和Post获取信息的入门示例
- 使用request.getInputStream() 获取POST数据
- nginx自定义模块编写-根据post参数路由到不同服务器
- 使用get,post,httpclient三种方式向服务器提交文本数据