首页 > 其他分享 >在lua中操作http请求有两种方式

在lua中操作http请求有两种方式

时间:2022-08-26 10:12:21浏览次数:74  
标签:resty http 请求 -- resp lua ngx

第一种方式:使用通过ngx.location.capture 去方式实现,但是有一些限制
第二种方式:因为openresty默认没有引入第三方http客户端类库lua-resty-http,需要下载(推荐)。

下载lua-resty-http类库

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

 

把http_headers.lua、http.lua文件放在D:\dev\openresty-1.19.9.1\lualib\resty目录,有重复直接覆盖。

location /test {
    default_type text/html;
    content_by_lua_file lua/http_test.lua;
}

 

D:\dev\openresty-1.19.9.1\lua\http_test.lua,内容如下:

local http = require("resty.http")
--创建http客户端实例
local httpc = http:new()

local resp,err = httpc:request_uri("http://issm.suning.com",
{
    method = "GET",
    path="/productDetail_P11271.htm",
    headers = {["User-Agent"]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"}
})
if not resp then
    ngx.say("request error:",err)
    return
end
--获取状态码
ngx.status = resp.status

--获取响应信息
--响应头中的Transfer-Encoding和Connection可以忽略,因为这个数据是当前server输出的。
for k,v in pairs(resp.headers) do
    if k ~= "Transfer-Encoding" and k ~= "Connection" then
        ngx.header[k] =v
    end
end

--响应体
ngx.say(resp.body)

httpc:close()

 

注意:需要在http模块中添加如下指令来做DNS解析,不然会报出解析域名错误,如下:

2022/08/26 09:33:21 [info] 8248#21972: *277 client timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while waiting for request, client: 127.0.0.1, server: 0.0.0.0:80

解决办法:resolver 8.8.8.8;

其http客户端也支持连接池,与redis、mysql连接池配置一致。

标签:resty,http,请求,--,resp,lua,ngx
From: https://www.cnblogs.com/linjiqin/p/16626630.html

相关文章

  • HTTPS 一定是安全的吗?
    大家好,我是小林。上周有位读者在面字节时被问道这么一个问题:HTTPS一定安全可靠吗?这个问题的场景是这样的:客户端通过浏览器向服务端发起HTTPS请求时,被「假基站」转发......
  • 使用PostMan请求Asp.Net Web Api接口错误,Swagger请求正常
    一、使用PostMan请求Asp.NetWebApi报错,如下图:二、使用Swagger请求Asp.NetWebApi正常,如下图:三、遇到如上问题,可能是以下两种问题导致1.先检查WebAPI接口的入参类......
  • 谷歌拼音自带lua
    functionfast_string_banji(argument) return{"快捷1","快捷2","快捷3","快捷4"}endfunctionfast_string_LDZY_DXXY(argument) return{"快捷1","快......
  • 1-2 django的app创建和说明+启动运行django+模板和静态文件+django的模板语法+请求和
    1.视频【1-6、1-7、1-8、1-9、1-10】https://www.bilibili.com/video/BV1S44y1K7Hd?p=6&spm_id_from=pageDriver 2.笔记 3.创建APP项目-app,用户管理【表结......
  • Lua注解及代码技巧
    Lua注解@class类声明注解EmmyLua利用@class注解来模拟面向对象中的类,可以继承,可以定义字段/属性完整格式:--@classMY_TYPE[:PARENT_TYPE][@comment]应用目......
  • [转]https://blog.csdn.net/potato1992/article/details/113729111
    注意:ubuntu默认没有装gcc,运行下面教程脚本前,先要装好gcc$sudoapt-getinstallgcc 原文连接:https://blog.csdn.net/potato1992/article/details/113729111 ......
  • ajax请求
    请求示例$.ajax({url:"/Home/GetAlarmPageData?state="+state+"&name="+name+"&index="+num,dataType......
  • axios请求响应拦截器的应用
    什么是axios拦截器?一般在使用axios时,会用到拦截器的功能,一般分为两种:请求拦截器、响应拦截器。请求拦截器在请求发送前进行必要操作处理例如添加统一cookie、请求体加......
  • 4个新的HTTP状态码含义:428、429、431、511
    1、428PreconditionRequired(要求先决条件)先决条件是客户端发送HTTP请求时,必须要满足的一些预设条件。一个好的例子就是If-None-Match头,经常用在GET请求中......
  • HTTP连环问
    HTTP连环问什么是HTTP?HTTP协议是HyperTextTransferProtocol(超文本传输协议)的缩写,是用于从万维网服务器传输超文本到本地浏览器的传送协议。HTTP特点?HTTP允许传......