lua发送http请求,luajit默认没有http.lua库,需要下载并存放到luajit对应目录。

一、下载http.lua和http_headers.lua库

参考:https://www.zixuephp.net/article-448.html
bash

location = /testscript{
    default_type text/plain;
    content_by_lua_file html/luafile/test.lua;
}

bash

vim test.lua
local zhttp = require "resty.http"
        1.运行后查看nginx错误日志,会提示没有http.lua文件:

2.下载http.lua和http_headers.lua库

下载页面:https://github.com/pintsized/lua-resty-http

直接下载:http_headers.lua-http.lua.rar

下载好后放入对应目录,这里的目录是:

bash

[root@zixuephp resty]# pwd
/usr/local/LuaJIT/share/luajit-2.0.5/resty
git clone https://github.com/pintsized/lua-resty-http.git

重启nginx。

二、lua发送http请求代码

1.get请求

bash

local zhttp = require "resty.http"
local function http_post_client(url, timeout)
        local httpc = zhttp.new()
 
        timeout = timeout or 30000
        httpc:set_timeout(timeout)
 
        local res, err_ = httpc:request_uri(url, {
                method = "GET",
                headers = {
                    ["Content-Type"] = "application/x-www-form-urlencoded",
                }
        })
        httpc:set_keepalive(5000, 100)
        --httpc:close()
        return res, err_
end
    2.post请求

bash

local zhttp = require "resty.http"
local function http_post_client(url,body,timeout)
        local httpc = zhttp.new()
 
        timeout = timeout or 30000
        httpc:set_timeout(timeout)
 
        local res, err_ = httpc:request_uri(url, {
                method = "POST",
                body = body,
                headers = {
                    ["Content-Type"] = "application/x-www-form-urlencoded",
                }
        })
        httpc:set_keepalive(5000, 100)
         httpc:close()
        if not res then 
            return nil, err_ 
         else if res.status == 200 then 
             return res.body, err_ 
         else 
             return nil, err_ end 
         end
 
end

bash

--get
local resp, err = http_post_client("http://zixuephp.net/index.html?name=test",3000)
--post
local body = {"name" = "test"}
local resp, err = http_post_client("http://zixuephp.net/index.html?name=test",body,3000)
————————————————
版权声明:本文为CSDN博主「wangc_gogo」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wangc_gogo/article/details/98318980

lua 发送http请求的更多相关文章

  1. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  2. AngularJs的$http发送POST请求,php无法接收Post的数据解决方案

      最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...

  3. Ajax发送POST请求SpringMVC页面跳转失败

    问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...

  4. 使用HttpClient来异步发送POST请求并解析GZIP回应

    .NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...

  5. 在发送ajax请求时加时间戳或者随机数去除js缓存

    在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...

  6. HttpUrlConnection发送url请求(后台springmvc)

    1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...

  7. kattle 发送post请求

    一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...

  8. 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?

    file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...

  9. 使用RestTemplate发送post请求

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

随机推荐

  1. .net跨域接口服务器端配置

    在项目Config文件中添加一下节点配置 <system.webServer> <httpProtocol> <customHeaders> <add nam ...

  2. python3中的nonlocal 与 global

    nonlocal 与 global nonlocal翻译是非本地,global翻译是全局,它们都是python3的新特性.如果以类C语言的思维去看这2个关键字,很可能觉得它们差不多.但实际上它们很不一 ...

  3. C语言变长数组

    #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Variable ...

  4. 三、CentOS 7.X系统安装配置超祥细图解教程

    一.CentOS7.7下载 官网下载地址:http://mirrors.163.com/centos 1.进入CentOS下载官网,找到CentOS7.4版本 2.在CentOS7.7版本页面中,找到 ...

  5. [一起面试AI]NO.9 如何判断函数凸或非凸

    首先定义凸集,如果x,y属于某个集合M,并且所有的θx+(1-θ)f(y)也属于M,那么M为一个凸集.如果函数f的定义域是凸集,并且满足 f(θx+(1-θ)y)≤θf(x)+(1-θ)f(y) 则该 ...

  6. git配置用户名

    git config --global user.name [name] git config --global user.email [email_address]

  7. L2 Softmax与分类模型

    softmax和分类模型 内容包含: softmax回归的基本概念 如何获取Fashion-MNIST数据集和读取数据 softmax回归模型的从零开始实现,实现一个对Fashion-MNIST训练集 ...

  8. 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom

    最长上升子序列o(nlongn)写法 dp[]=a[]; ; ;i<=n;i++){ if(a[i]>dp[len]) dp[++len]=a[i]; ,dp++len,a[i])=a[i ...

  9. 视频+图文 String类干货向总结

    目录 一.字符串的介绍及视频讲解 二.字符串的两种创建方式 方法一:通过new创建 方法二:直接创建 三.字符串的长度获取:length()方法 四.使用 == 和equals()方法比较两个字符串 ...

  10. TortoiseSVN的使用,以及冲突解决办法

    接下来,试试用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态, ...