lua获取GET 和 POST参数
function getpost()
local args = {}
local request_method = ngx.var.request_method
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == ngx.req.get_method() then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
return args
end
lua获取请求 body-适合获取http请求post和get参数【推荐】
ngx.req.get_body_data() 读请求体,会偶尔出现读取不到直接返回 nil 的情况。
所以得事先调用ngx.req.read_body(),强制本模块读取请求体
ngx.req.read_body()
local json_str = ngx.req.get_body_data()
--如果是json,则可以转化为table等等。。。
lua发起http get和post请求
function http_post(url,table_par)
local http = require "http"
local httpc = http.new()
local res, err = httpc:request_uri(url,{
method = 'POST',
body = cjson.encode(table_par),
headers = {["Content-Type"] = "application/x-www-form-urlencoded",}
})
if not res then
return 100
end
return res.body
end
function comm_fun:http_get(url,table_par)
local http = require "http"
local httpc = http:new()
local res, err = httpc:request_uri(url, {
method = "GET",
query = table_par,
--ssl_verify = false, -- 需要关闭这项才能发起https请求
headers = {["Content-Type"] = "application/x-www-form-urlencoded" },
})
if not res then
return 100
end
return res.body
end |