apache / apisix-java-plugin-runner

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

request help: i want to rewrite the response and then return to the client #232

Open 108825446 opened 1 year ago

108825446 commented 1 year ago

Issue description

I want to get the upstream response content in the plugin for processing and then return to the client, but it return null This is how the class is handled: @Override public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) { log.warn("ClientAuthenticationFilter is running");

    log.warn("status:{}", request.getUpstreamStatusCode());
   // get the upstream response body
    String responseBody = request.getBody();
    ResultData<ActivityLoginUser> resultData = JsonUtils.json2Bean(responseBody , ResultData.class);
    ResultData<AuthUserVo> authResult = new ResultData<>();
    authResult.setCode(resultData.getCode());
    authResult.setMessage(resultData.getMessage());
    if(ObjectUtils.isNotEmpty(resultData.getData())){
        AuthUserVo authUserVo = processAuthResult(resultData.getData());
        authResult.setData(authUserVo);
    }
    response.setBody(JsonUtils.bean2Json(authResult));
     chain.postFilter(request, response);
}

java.lang.NullPointerException: null postFilter(ClientAuthenticationFilter.java:85)

Environment

tzssangglass commented 1 year ago

com.taikang.tlife.activity.apisix.plugins.fliters.ClientAuthenticationFilter.postFilter(ClientAuthenticationFilter.java:85)

Is this the NPE in your private code?

108825446 commented 1 year ago

yes, it the plugin of the authentication, the implement fo the plugin is like below:

@Component public class ClientAuthenticationFilter implements PluginFilter {

@Override
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
    log.warn("ClientAuthenticationFilter is running");
    log.warn("status:{}", request.getUpstreamStatusCode());

    **//i want to get the upstream body, but it cant ,  the reponseBody is null, so it throws the NullPointerException**
    String responseBody = request.getBody();

    String authResult  = processAuthResult(responseBody );

    response.setBody(authResult  );
    chain.postFilter(request, response);
}
tzssangglass commented 1 year ago

whihe line cause the NPE? You need to give detailed steps for reproduction.

108825446 commented 1 year ago

The line String responseBody = request.getBody(); the responseBody is null, when process it in processAuthResult, it throws NullPointerException. I want to know how to get the upstream reponse content. because the " request.getBody()" cant get the upstream body.

tzssangglass commented 1 year ago

the responseBody is null, when process it in processAuthResult, it throws NullPointerException.

You need to give detailed steps for reproduction.

tzssangglass commented 1 year ago

a good reproduct describtion like: https://github.com/apache/apisix-java-plugin-runner/issues/189#issuecomment-1226756930

gaoxingliang commented 9 months ago

do you add those lines?

    /**
     * If you need to fetch request body in the current plugin, you will need to return true in this function.
     */
    @Override
    public Boolean requiredRespBody() {
        return true;
    }

    /**
     * If you need to fetch request body in the current plugin, you will need to return true in this function.
     */
    @Override
    public Boolean requiredBody() {
        return true;
    }