- openresty默认没有提供http客户端,需要第三方提供插件
- 获取方式
https://github.com/ledgetech/lua-resty-http/tree/master/lib/resty
获取http_headers.lua和http.lua
将文件放在 /openresty/lualib/resty/目录下即可
- 使用方式
local res, err = httpc:request_uri(uri, {
method = "POST/GET", ---请求方式
query = str, ---get方式传参数
body = str, ---post方式传参数
path = "url" ----路径
headers = { ---header参数
["Content-Type"] = "application/json",
}
})
- multipart/form-data编码
local boundary = "alphabeticstring" .. ngx.md5(ngx.now())
local req_body = "------" .. boundary .. "\r\n" ..
'Content-Disposition: form-data; name="name"\r\n\r\n{"key":"value"}\r\n' ..
"------" .. boundary .. "--"
local resp, err = httpc:request_uri(uri, {
method = "POST", ---请求方式POST
path = "url", ----路径
headers = { ---header参数
["Content-Type"] = "multipart/form-data; boundary=----" .. boundary,
["Content-Length"] = #req_body
},
body = req_body --请求体
})
- application/x-www-form-urlencoded编码
local resp,err = httpc:request_uri(uri, {
method = "POST", --请求方式POST
path = url, --路径
headers = { --header参数
["Content-Type"] = "application/x-www-form-urlencoded"
},
body = "key1=" .. value1 .. "&key2=" .. value2 --请求体,value1和value2为参数值
})
常见问题
1.没有dns解析器解析域名
1.2024/01/29 16:21:54 [error] 13665#0: *326678630 [lua] aes_auth.lua:44: aes_auth(): no resolver defined to resolve
"abc-uat.xxx.com.cn", client: 10.1.1.118
需要在nginx.conf配置文件中增加dns服务地址:
resolver 10.1.x.x;
2.不能通过ssl认证
[error] 22823#0: *326683191 lua ssl certificate verify error: (20: unable to get local issuer certificate),
client: 10.1.1.118, server: abc-uat.xxx.com.cn
需要在nginx.conf配置文件中增加以下配置:
lua_ssl_trusted_certificate "/etc/pki/tls/certs/ca-bundle.crt";
lua_ssl_verify_depth 3;
如何确定证书的位置:
curl -v https://abc-uat.xxx.com.cn