Custom Lua Plugin in Kong Failed to Properly Send HTTP Request to Spring Boot Rest API (or the other way around)

I’ve been having this problem in weeks. I made a custom plugin for Kong, written in Lua, that supposed to forward any request that Kong received to a custom auth service. Here is Handler.lua.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>-- Handler.lua
local http = require "resty.http"
local utils = require "kong.tools.utils"
local cjson = require("cjson")
local MessageForwarder = {
VERSION = "1.0",
PRIORITY = 1000,
}
local function forward_message(conf, request_method, request_path, request_headers, request_body)
-- Make HTTP Request
local httpc = http.new()
local res, err = httpc:request_uri(conf.forward_host .. request_path, {
method = request_method,
headers = request_headers,
body = request_body,
ssl_verify = false
})
-- Checking response
if not res then
kong.log.err("Failed to forward request: ", err)
return kong.response.exit(500)
end
if res.status ~= 200 then
kong.log.err("Responded with status: ",res.status)
return kong.response.exit(500)
end
return true -- all is well
end
local function forward_message_new(conf, request_method, request_path, request_headers, request_body)
local server = assert(socket.bind("*", 0))
end
function MessageForwarder:access(conf)
local request_path = kong.request.get_raw_path()
local request_method = kong.request.get_method()
local request_headers = kong.request.get_headers()
local raw_request_body
if raw_request_method ~= "GET" then
raw_request_body = kong.request.get_raw_body()
end
local request_body = cjson.decode(raw_request_body)
local auth_path = conf.auth_token_b2b_endpoint
local bool = forward_message(conf, request_method, request_path, request_headers, request_body)
return true
end
return MessageForwarder
</code>
<code>-- Handler.lua local http = require "resty.http" local utils = require "kong.tools.utils" local cjson = require("cjson") local MessageForwarder = { VERSION = "1.0", PRIORITY = 1000, } local function forward_message(conf, request_method, request_path, request_headers, request_body) -- Make HTTP Request local httpc = http.new() local res, err = httpc:request_uri(conf.forward_host .. request_path, { method = request_method, headers = request_headers, body = request_body, ssl_verify = false }) -- Checking response if not res then kong.log.err("Failed to forward request: ", err) return kong.response.exit(500) end if res.status ~= 200 then kong.log.err("Responded with status: ",res.status) return kong.response.exit(500) end return true -- all is well end local function forward_message_new(conf, request_method, request_path, request_headers, request_body) local server = assert(socket.bind("*", 0)) end function MessageForwarder:access(conf) local request_path = kong.request.get_raw_path() local request_method = kong.request.get_method() local request_headers = kong.request.get_headers() local raw_request_body if raw_request_method ~= "GET" then raw_request_body = kong.request.get_raw_body() end local request_body = cjson.decode(raw_request_body) local auth_path = conf.auth_token_b2b_endpoint local bool = forward_message(conf, request_method, request_path, request_headers, request_body) return true end return MessageForwarder </code>
-- Handler.lua

local http = require "resty.http"
local utils = require "kong.tools.utils"
local cjson = require("cjson")

local MessageForwarder = {
    VERSION = "1.0",
    PRIORITY = 1000,
}

local function forward_message(conf, request_method, request_path, request_headers, request_body)
  -- Make HTTP Request
  local httpc = http.new()
  local res, err = httpc:request_uri(conf.forward_host .. request_path, {
    method = request_method,
    headers = request_headers,
    body = request_body,
    ssl_verify = false
  })

  -- Checking response
  if not res then
    kong.log.err("Failed to forward request: ", err)
    return kong.response.exit(500)
  end

  if res.status ~= 200 then
    kong.log.err("Responded with status: ",res.status)
    return kong.response.exit(500)
  end

  return true -- all is well
end

local function forward_message_new(conf, request_method, request_path, request_headers, request_body)
  local server = assert(socket.bind("*", 0))
end

function MessageForwarder:access(conf)
  local request_path = kong.request.get_raw_path()
  local request_method = kong.request.get_method()
  local request_headers = kong.request.get_headers()

  local raw_request_body
  if raw_request_method ~= "GET" then
    raw_request_body = kong.request.get_raw_body()
  end

  local request_body = cjson.decode(raw_request_body)

  local auth_path = conf.auth_token_b2b_endpoint

  local bool = forward_message(conf, request_method, request_path, request_headers, request_body)

  return true
end


return MessageForwarder

The result is always consistent. Consistent errors. The Spring Boot REST API that receives the request forwarded from that plugin returns this error to the Spring Boot log:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message]
</code>
<code>WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message] </code>
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message]

I’m not sure if the problem is in my Spring Boot API since I’ve tested it with requests from other sources (Postman, Insomnia, other Spring Boot app, Ajax) and everything works fine, just not from my custom Kong plugin with Lua. I wonder if there are some set of rules in using resty.http that I didn’t follow.

Any suggestion would be highly appreciated.

As an additional information, I adapted this tutorial from Kong blog to fit my requirements: https://konghq.com/blog/engineering/custom-authentication-and-authorization-framework-with-kong

New contributor

Mehdi. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật