apache / apisix-java-plugin-runner

APISIX Plugin Runner in Java
https://apisix.apache.org/
Apache License 2.0
130 stars 96 forks source link

request help: Java 自定义插件,实现postFilter,但是无法获取上游服务响应的数据 apisix-version :2.15 #273

Closed Forever-ZF closed 11 months ago

Forever-ZF commented 11 months ago

apisix-version :2.15 现在有个上游服务响应数据 是一串字符串。如 :post请求

插件代码如下

public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) { System.out.println("执行了 ext-plugin-post-resp 插件"); String configStr = request.getConfig(this); String json = null; Gson gson = new Gson(); Map<String, Object> conf = new HashMap<>(); conf = gson.fromJson(configStr, conf.getClass()); String param = (String) conf.get("param"); System.out.println("param参数---------" + param + "--------------"); String body = request.getBody(); ObjectMapper objectMapper = new ObjectMapper(); try { System.out.println("request--------" +objectMapper.writeValueAsString(request)); System.out.println("request.getBody()--------" +request.getBody()); System.out.println("request.getUpstreamHeaders()--------" +objectMapper.writeValueAsString(request.getUpstreamHeaders())); System.out.println("response--------" +objectMapper.writeValueAsString(response)); } catch (Exception e) { throw new RuntimeException(e); } chain.postFilter(request, response); }

日志如下

2023/10/30 06:16:39 [warn] 48#48: 88 [lua] init.lua:913: 执行了 ext-plugin-post-resp 插件 param参数---------username;telephone-------------- , context: ngx.timer 2023/10/30 06:16:39 [warn] 48#48: 88 [lua] init.lua:913: request--------{"requestId":8388608,"body":null,"upstreamHeaders":{"Keep-Alive":"timeout=60","Connection":"keep-alive","Content-Length":"10","Date":"Mon, 30 Oct 2023 06:17:38 GMT","Content-Type":"text/plain;charset=UTF-8"},"confToken":1047925334,"upstreamStatusCode":200,"type":4} request.getBody()--------null request.getUpstreamHeaders()--------{"Keep-Alive":"timeout=60","Connection":"keep-alive","Content-Length":"10","Date":"Mon, 30 Oct 2023 06:17:38 GMT","Content-Type":"text/plain;charset=UTF-8"} response--------{"errResponse":null,"type":4}

request中的"Content-Length":"10" 对应 post请求 是准确的,但是无法获取对应信息,该怎么解决?

githubxubin commented 11 months ago

@tzssangglass 我也遇到这个问题了 请问是版本问题还是bug呀 我的是 plugin -runner 0.4 APISIX-2.15 请回复一下 谢谢

Forever-ZF commented 11 months ago

2.15版本就是如此,3.0版本新增获取响应体