openresty使用http模块调用http接口

  • 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值