You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "cb123456-user (via GitHub)" <gi...@apache.org> on 2024/04/12 02:44:06 UTC

[I] request help: 插件获取ext-plugin-post-resp获取请求响应码为0 [apisix-java-plugin-runner]

cb123456-user opened a new issue, #307:
URL: https://github.com/apache/apisix-java-plugin-runner/issues/307

   ### Issue description
   只配置了自定义插件apisix-java-plugin-runner,使用request.getVars("status")获取到的响应码为0,期望获取的是200,应该怎么获取正确的响应码?
   
   ### Environment
   APISIX version:3.2
   apisix-java-plugin-runner version:0.4.0
   
   ###  plugin config
   "plugins": {
   		"ext-plugin-post-resp": {
   			"allow_degradation": true,
   			"conf": [{
   				"name": "ReceiveResponseFilter",
   				"value": "{\"appId\":\"1000\",\"validate_header\":\"cookie\",\"rejected_code\":\"403\"}"
   			}]
   		}
   	}
   
   ### 插件代码
   @Slf4j
   @Component
   public class ReceiveResponseFilter implements PluginFilter {
   
       @Override
       public String name() {
           return "ReceiveResponseFilter";
       }
   
       /**
        * @param request
        * @param response
        * @param chain
        */
       @Override
       public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
           String configStr = request.getConfig(this);
           Gson gson = new Gson();
           Map<String, Object> conf = new HashMap<>();
           conf = gson.fromJson(configStr, conf.getClass());
   
           try {
               String appId = String.valueOf(conf.get("appId"));
               log.info("ReceiveResponseFilter appId {}", appId);
   
               byte[] bodyBytes = request.getBody().getBytes(StandardCharsets.ISO_8859_1);
               String responseBody = new String(bodyBytes, StandardCharsets.UTF_8);
               log.info("responseBody----- {}", responseBody);
   
               String requestVarsHost = request.getVars("host");
               String uri = request.getVars("uri");
               String request_method = request.getVars("request_method");
               String remote_addr = request.getVars("remote_addr");
               String http_cookie = request.getVars("http_cookie");
               String status = request.getVars("status");
               log.info("requestVars host {}", requestVarsHost);
               log.info("requestVars uri {}", uri);
               log.info("requestVars request_method {}", request_method);
               log.info("requestVars remote_addr {}", remote_addr);
               log.info("requestVars http_cookie {}", http_cookie);
               log.info("requestVars status {}", status);
   
               Integer upstreamStatusCode = request.getUpstreamStatusCode();
               log.info("upstreamStatusCode {}", upstreamStatusCode);
   
           } catch (Exception e) {
               log.info("ReceiveResponseFilter error {}", ExceptionUtil.stacktraceToString(e));
           }
           chain.postFilter(request, response);
       }
   
       @Override
       public List<String> requiredVars() {
           List<String> requiredVars = new ArrayList<>();
           requiredVars.add("host");
           requiredVars.add("uri");
           requiredVars.add("request_method");
           requiredVars.add("remote_addr");
           requiredVars.add("http_cookie");
           requiredVars.add("status");
   
           return requiredVars;
       }
   
       @Override
       public Boolean requiredRespBody() {
           return true;
       }
   }
   
   ### 正常请求结果
   requestVars status 0
   upstreamStatusCode 13
   
   ### 失败请求结果
   requestVars status 0
   upstreamStatusCode 13
   
   ### 预期结果
   正常获取请求返回的成功/失败响应码


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] request help: 插件获取ext-plugin-post-resp获取请求响应码为0 [apisix-java-plugin-runner]

Posted by "gaoxingliang (via GitHub)" <gi...@apache.org>.
gaoxingliang commented on issue #307:
URL: https://github.com/apache/apisix-java-plugin-runner/issues/307#issuecomment-2677749877

   @cb123456-user 
   I encountered this issue previous. and fix it:
   See the comment here:
   https://github.com/gaoxingliang/apisix-java-plugin-runner/blob/encrypt-response/readme-new.md?plain=1#L9
   
   But the problem here is: I forgot how to fix that. You can try to upgrade the dependency like this:
   ```
               <groupId>io.github.api7</groupId>
               <artifactId>A6</artifactId>
               <version>0.6.1-RELEASE</version>
   ```
   And what I can confirm is: the code in my repo is working as expected. In the post response code, I also checked the upstream status:
   
   https://github.com/gaoxingliang/apisix-java-plugin-runner/blob/encrypt-response/runner-starter/src/main/java/org/apache/apisix/plugin/runner/filter/EncryptResponseFilter.java#L46


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org