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. linux中的隐藏权限,chattr,lsattr

    chattr chattr可以用来制约root的权限,使得系统更加安全. 主要参数: a:让文件或目录仅供附加用途. b:不更新文件或目录的最后存取时间. c:将文件或目录压缩后存放. d:将文件或目 ...

  2. Matlab入门(一)

    1.常用命令 cd 显示或改变当前工作目录 load 加载指定文件的变量 dir 显示当前目录或指定目录下的文件 diary 日志文件命令 clc 清除工作窗中的所有显示内容 ! 调用 DOS 命令 ...

  3. "强调内容"组件:<em> —— 快应用组件库H-UI

     <import name="em" src="../Common/ui/h-ui/text/c_tag_i"></import> & ...

  4. Kitty-Cloud服务搭建过程剖析

    项目地址 https://github.com/yinjihuan/kitty-cloud 服务搭建 大家目前看到的都是我已经搭建好了的服务,如果让你从零开始自己搭建一个微服务的项目,要怎么做? 我们 ...

  5. JS入门系列(2)-原型-实例属性

    下面的例子中,在构造器内部定义了like,然后再原型上也定义了like.通过下面的测试表明: 在构造器内部创建的实例方法会阻挡原型上定义的同名方法 初始化操作的优先级如下: 首先,通过原型给对象实例添 ...

  6. 萌新带你开车上p站(三)

    本文作者:萌新 前情回顾: 萌新带你开车上p站(一) 萌新带你开车上p站(二) 0x08 题目给的提示是和运算符优先级有关 登录后直接看源码 mistake@pwnable:~$ ls flag mi ...

  7. Golang——详解Go语言代码规范

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Golang专题的第二篇,我们来看看Go的语言规范. 在我们继续今天的内容之前,先来回答一个问题. 有同学在后台问我,为什么说Gola ...

  8. F - Robot Motion 栈加BFS

    A robot has been programmed to follow the instructions in its path. Instructions for the next direct ...

  9. 3. JS生成32位随机数

    function randomWord ( randomFlag,min,max ) { var str = " ", range = min, arr = ['0','1','2 ...

  10. github使用--配置sshkey

    配置sshkey 好处:每次提代码都不要输入用户名和密码. 1.生成key: 输入命令ssh-keygen -t rsa -C "邮箱地址" [邮箱地址:注册github时填写的邮 ...