1

I am facing an issue while testing a sample code using resty cli. Please find the output of related commands:

$ which resty
/usr/local/bin/resty
$ which openresty
/usr/local/bin/openresty
$ resty -e 'ngx.say("hello world")'               
hello world

File: test.lua

local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://example.com/helloworld", {
  method = "POST",
  body = "a=1&b=2",
  headers = {
    ["Content-Type"] = "application/x-www-form-urlencoded",
  },
  keepalive_timeout = 60000,
  keepalive_pool = 10
})

if not res then
  ngx.say("failed to request: ", err)
  return
end

While running this test.lua file using resty cli, I am facing module 'resty.http' not found:

$ resty test.lua                                    
ERROR: test.lua:1: module 'resty.http' not found:
        no field package.preload['resty.http']
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http/init.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http/init.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http/init.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http/init.lua'
        no file './resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
        no file '/usr/local/share/lua/5.1/resty/http.lua'
        no file '/usr/local/share/lua/5.1/resty/http/init.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/share/lua/5.1/resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/share/lua/5.1/resty/http/init.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.so'
        no file './resty/http.so'
        no file '/usr/local/lib/lua/5.1/resty/http.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/lib/lua/5.1/resty/http.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty.so'
        no file './resty.so'
        no file '/usr/local/lib/lua/5.1/resty.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/lib/lua/5.1/resty.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        test.lua:1: in function 'file_gen'
        init_worker_by_lua:45: in function <init_worker_by_lua:43>
        [C]: in function 'xpcall'
        init_worker_by_lua:52: in function <init_worker_by_lua:50>

I have tried Install resty.http with already installed openresty also.

Rishabh Sairawat
  • 440
  • 6
  • 17

2 Answers2

0

I solved it using the same solution mentioned in Install resty.http with already installed openresty.

The first time, it wasn't working for me because of my resty path. But when I checked the error log, I came to know that It was looking for http.lua at /usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.lua So copying https://github.com/ledgetech/lua-resty-http/blob/master/lib/resty/http.lua worked for me.

Thanks to Alexander Altshuler for the original answer

Rishabh Sairawat
  • 440
  • 6
  • 17
-1

That library comes with Kong by default. Instead of copying the files adjust your Lua path settings using the LUA_PATH environment variable. This can be done using LuaRocks (also included by default with Kong)

To set up the paths using LuaRocks try:

eval `luarocks path`
Tieske
  • 363
  • 3
  • 10
  • I wasn't using kong. I was using open resty command line – Rishabh Sairawat Jan 07 '21 at 10:54
  • 1
    Not sure why the answer was down voted. @RishabhSairawat you tagged the question with `kong` hence I assume you are trying this on a system with Kong installed? If you are on a Kong system the answer is valid. See the Kong rockspec: https://github.com/Kong/kong/blob/66c4e5fbb49ddf42026f987f7ae84ccb6cbbee4c/kong-2.2.1-0.rockspec#L18 – Tieske Jan 09 '21 at 23:18
  • question specifically asked for resty cli – Rishabh Sairawat Jan 25 '21 at 11:01