Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3643

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

$
0
0

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.

-- Handler.lualocal 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 wellendlocal function forward_message_new(conf, request_method, request_path, request_headers, request_body)  local server = assert(socket.bind("*", 0))endfunction 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 trueendreturn 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:

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 messageWARN 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


Viewing all articles
Browse latest Browse all 3643

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>