title: Spring Boot + Zuul: how to get the "Content-length" header from a file stream provided by RESTful services?
body: <p>I am using Spring Cloud and Zuul proxy as gateway to my RESTful service provided by a microservice. When I perform a request directly to an instance of a microservice, all the headers are provided as I expected. However, when the same request is proxied by Zuul, the header "Content-length" is removed. I've made some research about it and I saw that Zuul adds the header "Transfer-Encoding" as "chunked" and in this case the header Content-length is omitted (<a href="https://stackoverflow.com/questions/5553151/content-length-is-being-stripped">Content-Length is being stripped</a>, <a href="https://stackoverflow.com/questions/32084831/spring-cloud-netflix-whats-happening-in-zuulconfiguration-with-the-zuulservlet">Spring Cloud Netflix: Whats happening in ZuulConfiguration with the ZuulServlet?</a>). </p>
  
  <p>However, I really need to get the "Content-length" provided by my RESTful service. This request also must be proxied by Zuul (I have many instances of a microservice, so I wouldn't access them directly). </p>
  
  <p>Here is the method in my microservice:</p>
  
  <pre><code>@RequestMapping(value = "/jobresult/{id}", method = RequestMethod.GET)
  @Timed
  public ResponseEntity&lt;InputStreamResource&gt; downloadJobResult(@PathVariable Long id) {
      Job job = jobService.findOne(id);
      File file = new File(job.getTargetFile());
      try {
         return ResponseEntity.ok().contentLength(file.length()).contentType(MediaType.APPLICATION_OCTET_STREAM).body(new InputStreamResource(new FileInputStream(file)));
      } catch (FileNotFoundException e) {
         log.error(e.getMessage(), e);
      }
  }
  </code></pre>
  
  <p>For example, the request to /api/jobresult/1 provides the header "Content-length" correctly, but the request to /service/api/jobresult/1 (routed by Zuul) do not show this header and also modify the "Transfer-Encoding" to "chunked".  </p>