使用过程
- 1.下载resty.http 库
- 2.将lib/resty文件夹下的http.lua 和http_headers.lua两个文件复制到
openresty的lualib/resty 目录下
常见报错
1. no resolver defined to resolve
解决办法
在location标签下添加
resolver 8.8.8.8;
2. unable to get local issuer certificate
解决办法
//关闭ssl检验
ssl_verify =false
示例
local http = require("resty.http")
local httpc = http:new()
local res,err = httpc:request_uri("https://www.baidu.com",{
method = "get",
body = "a=1&b=2",
ssl_verify=false,
headers={
["Content-Type"] = "application/x-www-form-urlencoded"
}
})
if not res then
ngx.say("fail to request ",err)
else
ngx.say(res.body)
end
api解析
1.创建http对象 http:new
2.发起请求 httpc:request_uri(uri,params)
params支持的参数
- method :请求类型,一般设置为get或者post
- headers :请求头,类型为table
- ssl_verify :是否开启ssl验证
-query: 请求参数 字符串类型
本文介绍了如何在OpenResty中使用resty.http库进行HTTP请求,包括下载库、复制文件到指定目录、处理常见错误如DNS解析问题和SSL证书问题。示例代码展示了发起GET请求、关闭SSL验证和设置请求头的方法。
2654

被折叠的 条评论
为什么被折叠?



