使用http模块
cd /usr/local/openresty/lualib/resty
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lualocal res, err = httpc:request_uri(uri, {
method = "POST/GET", ---请求方式
query = str, ---get方式传参数
body = str, ---post方式传参数
path = "url" ----路径
headers = { ---header参数
["Content-Type"] = "application/json",
}
})--引入http模块
local http = require("resty.http")
--创建http客户端实例
local httpc = http.new()
--request_uri函数请求淘宝
local resp, err = httpc:request_uri("https://s.taobao.com", {
method = "GET", ---请求方式
query = "q=iphone&b=2", ---get方式传参数
body = "c=3&d=4", ---post方式传参数
path = "/search", ----路径
headers = { ---header参数
["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) ",
["token"] = "1234456"
}
})
if not resp then
ngx.say("request error :", err)
return
end
--获取返回的状态码
ngx.status = resp.status
if ngx.status ~= 200 then
ngx.log(ngx.WARN,"非200状态,ngx.status:"..ngx.status)
return resStr
end
--获取遍历返回的头信息
for k, v in pairs(resp.headers) do
if type(v) == "table" then
ngx.log(ngx.WARN,"table:"..k, ": ", table.concat(v, ", "))
else
ngx.log(ngx.WARN,"one:"..k, ": ", v)
end
end
--响应体
ngx.say(resp.body)
httpc:close()Last updated