apisix-java-plugin-runner icon indicating copy to clipboard operation
apisix-java-plugin-runner copied to clipboard

request help: how should i get the custom header (this header was added beforehand in an ext-plugin-pre-req plugin.filter method) in ext-plugin-post-resp plugin???

Open xuexue6282 opened this issue 3 years ago • 5 comments

Issue description

I added an request header (startTimestamp) in an ext-plugin-pre-req plugin using HttpRequest.setHeader() method, then I want to acquire this custom header in an ext-plugin-post-resp plugin using PostRequest.getUpstreamHeaders() method. But it doesn't work. I've tried serveral ways, and none of them worked. This problem bothered me a lot, could you help me? Really thanks.

Environment

  • apisix-java-plugin-runner version 0.3.0
  • apisix verison 2.15.1

xuexue6282 avatar Jan 14 '23 18:01 xuexue6282

HttpRequest.setHeader is to set the request header, and PostRequest.getUpstreamHeaders gets the response header, and there is no direct connection between them.

nic-chen avatar Jan 15 '23 12:01 nic-chen

So is there any way to solve this problem? Thanks for any given idea.

--

发自新浪邮箱客户端

在 1月15日 20:56,JunXu Chen @.***> 写道:

HttpRequest.setHeader is to set the request header, and PostRequest.getUpstreamHeaders gets the response header, and there is no direct connection between them. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

xuexue6282 avatar Jan 15 '23 13:01 xuexue6282

hi @christagger

Could you give a minimal example? I don't know exactly what your specific scenario is.

nic-chen avatar Jan 16 '23 03:01 nic-chen

here is my code, i want to calculate the request-response time :

public class DemoFilter implements PluginFilter {
    private final Logger logger = LoggerFactory.getLogger(DemoFilter.class);

    @Override
    public String name() {
        return "DemoFilter";
    }

    @Override
    public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
        logger.info("DemoFilter is running");
        // TODO : add a custom header "startTimestamp" here (or added in another plugin's filter() method)
        request.setHeader("startTimestamp", String.valueOf(System.currentTimeMillis()));
        chain.filter(request, response);
    }

    @Override
    public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
        // TODO : i want to get the custom header "startTimestamp" here, so i can
        //  calculate the request-response time
        chain.postFilter(request, response);
    }

    /**
     * If you need to fetch some Nginx variables in the current plugin, you will need to declare them in this function.
     * @return a list of Nginx variables that need to be called in this plugin
     */
    @Override
    public List<String> requiredVars() {
        List<String> vars = new ArrayList<>();
        vars.add("remote_addr");
        vars.add("server_port");
        return vars;
    }

    /**
     * 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;
    }
}

Or the more common scenario is: the custom header added in a pre-req-plugin can be passed all along the request and can be acquired in every post-resp-plugin postFilter() method.

xuexue6282 avatar Jan 16 '23 04:01 xuexue6282

maybe you could try to add the header to requireVars, like:

    public List<String> requiredVars() {
        List<String> vars = new ArrayList<>();
        vars.add("remote_addr");
        vars.add("server_port");
		vars.add("http_ startTimestamp");
        return vars;
    }

nic-chen avatar Feb 06 '23 03:02 nic-chen