title: How to do content based dynamic routing in zuul?
body: <p>I am working on a micro-services based architecture which includes Spring-Boot, Eureka, Zuul at key level. My problem is as follows:</p>
  
  <p>Service1 : /api/v1/service1 POST
  <br/>
  Service2 : /api/v2/service2 POST</p>
  
  <p>application.yml looks like</p>
  
  <pre>
  zuul:
      routes:
          service1:
              path: /api/v1/**
          service2:
              path: /api/v1/**
          common:
              path: /common/endpoint
  </pre>
  
  <p>Also I have written a filter where I am trying to take input via this common endpoint and deduct based on post request where to enroute the request i.e. to service1 or service2. But here I am stuck and nothing seems to work out, even after several google searches and checking out other people's problem I am not yet able to found my solution.
  here is how my Filter looks like:</p>
  
  <pre><code>
  public class CommonEndpointFilter extends ZuulFilter {
  
      @Override
      public boolean shouldFilter() {
          RequestContext ctx = RequestContext.getCurrentContext();
          if ((ctx.get("proxy") != null) && ctx.get("proxy").equals("common")) {
              return true;
          }
          return false;
      }
  
      @Override
      public Object run() {
          RequestContext ctx = RequestContext.getCurrentContext();
          HttpServletRequest request = ctx.getRequest();
  
          InputStream in = (InputStream) ctx.get("requestEntity");
          if (in == null) {
              try {
                  in = request.getInputStream();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          String body = null;
          try {
              body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
  
              if (body.indexOf("somethingrelatedtoservice1") != -1) {
                  //forward the request to Service1: /api/v1/service1
              } else if (body.indexOf("somethingrelatedtoservice2") != -1) {
                  //forward the request to Service2: /api/v1/service2
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public String filterType() {
          return FilterConstants.ROUTE_TYPE;
      }
  
      @Override
      public int filterOrder() {
          return 0;
      }
  }
  </code></pre>
  
  <p>I am not able to figure out how to forward the request further to those services and then obtain response to send back via common endpoint.</p>
  
  <p>What am I doing wrong here ? How can I proceed so I don't have to use hardcoded url of any of the service. Please guide me through this.</p>
  
  <p>I have tried out some of the things like:
  <br/>
  1. using DiscoveryClient to obtain instance and use setRouteHost method of context. It always throws a zuulfilter exception URL is not proper common/endpoint is appended after the url of service obtained via DiscoveryClient.<br/>
  2. I tried stupidest thing that came to mind, using RestTemplate to make a request and obtain response and put that in context response which didn't work either, however request was forwarded to the service but I wouldn't receive any response.</p>
  
  <p>Any help is appreciated!!</p>