openresty之http

本文介绍了如何在OpenResty中使用resty.http库进行HTTP请求,包括下载库、复制文件到指定目录、处理常见错误如DNS解析问题和SSL证书问题。示例代码展示了发起GET请求、关闭SSL验证和设置请求头的方法。
摘要由CSDN通过智能技术生成

使用过程

  • 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: 请求参数 字符串类型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值