lua http request is sent, no http.lua luajit default library, you need to download and place the luajit the corresponding directory.
First, download http.lua and http_headers.lua library
Reference: HTTPS: //www.zixuephp.net/article-448.html
bash
location = /testscript{
default_type text/plain;
content_by_lua_file html/luafile/test.lua;
}
bash
test.lua vim
local zhttp = The require "resty.http"
1. Run View nginx error log file will prompt no http.lua:
2. Download http.lua and http_headers.lua library
Download page: https: //github.com/pintsized/lua-resty-http
Direct Download: http_headers.lua-http.lua.rar
Once you have downloaded into the corresponding directory, the directory is here:
bash
[root@zixuephp resty]# pwd
/usr/local/LuaJIT/share/luajit-2.0.5/resty
git clone https://github.com/pintsized/lua-resty-http.git
Restart nginx.
Two, lua http request code transmitted
1.get request
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_ than
than
than
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) ----------------
Disclaimer: This article is CSDN bloggers' wangc_gogo "the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/wangc_gogo/article/details/98318980