You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
Lua
51 lines
1.1 KiB
Lua
local request = {}
|
|
local http = require "resty.http"
|
|
|
|
-- http请求函数
|
|
function request.get(url)
|
|
local httpc = http.new()
|
|
local methods = "GET"
|
|
local res, err = httpc:request_uri(url, {
|
|
ssl_verify = false,
|
|
method = methods,
|
|
headers = {
|
|
["Content-Type"] = "application/x-www-form-urlencoded",
|
|
}
|
|
})
|
|
if not res then
|
|
ngx.log(ngx.INFO,"failed to request: ", err)
|
|
return false
|
|
else
|
|
if res.status == 200 then
|
|
return res.body
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
end
|
|
|
|
function request.post(url, body, headers)
|
|
local httpc = http.new()
|
|
local body = body or ""
|
|
local headers = headers or {
|
|
["Content-Type"] = "application/json",
|
|
}
|
|
local res, err = httpc:request_uri(url, {
|
|
ssl_verify = false,
|
|
method = "POST",
|
|
body = body,
|
|
headers = headers
|
|
})
|
|
if not res then
|
|
ngx.log(ngx.INFO,"failed to request: ", err)
|
|
return false
|
|
else
|
|
if res.status == 200 then
|
|
return res.body
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
end
|
|
|
|
return request |