http客户端
Openresty没有提供默认的Http客户端,需要下载第三方的http客户端。
下载lua-resty-http到lualib目录下,使用以下的命令下载:
cd /usr/example/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.lua
lua-resty-http模块的地址为https://github.com/pintsized/lua-resty-http
安装成功后,通过require(“resty.http”)引入 lua_http模块,它有以下的api方法:
- syntax: httpc = http.new() 创建一个 http对象
- syntax: res, err = httpc:request_uri(uri, params)根据参数获取内容,包括:
- status 状态码
- headers 响应头
- body 响应体
vim /usr/example/lua/test_http.lua,写以下代码:
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri("http://s.taobao.com", {
method