title: Spring Cloud Gateway not returning correct Response code given by Downstream service (for file upload)
body: <p>I have a simple downstream service for file upload. Sample code</p>
  
  <pre><code>@RestController
  @RequestMapping("/file")
  public class FileController {
  
      @PostMapping("/upload")
      public ResponseEntity&lt;?&gt; uploadFile(@RequestParam("file") MultipartFile file,
          @RequestParam(value = "delay", required = false, defaultValue = "0") int delay) throws Exception {
  
          System.out.println(String.join(System.getProperty("line.separator"),
              "File Name =&gt; " + file.getOriginalFilename(),
              "File Size =&gt; " + file.getSize() + "bytes",
              "File Content Type =&gt; " + file.getContentType()));
  
          TimeUnit.MILLISECONDS.sleep(delay);
  
          return ResponseEntity.ok(file.getName() + " uploaded");
      }
  
  }
  </code></pre>
  
  <p>and a <code>CustomExceptionHandler</code> that returns <code>BAD_REQUEST</code> if there is a <code>MultipartException</code>:</p>
  
  <pre><code>@Configuration
  @ControllerAdvice
  public class CustomExceptionHandler {
      @ExceptionHandler(MultipartException.class)
      public ResponseEntity&lt;String&gt; handleMultipartException(MultipartException ex) {
          return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
      }
  }
  </code></pre>
  
  <p>The size limit is 10MB in application.yml:</p>
  
  <pre><code>spring:
    servlet:
      multipart:
        max-file-size: 10MB
        max-request-size: 10MB
  </code></pre>
  
  <p>If I upload a large file, it gives me a a <code>400</code> status as expected
  <a href="https://i.stack.imgur.com/WSuoj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WSuoj.png" alt="File serivce responsing 400"></a></p>
  
  <p>When I try to hit the same via <strong>spring cloud gateway</strong> I get the following result:</p>
  
  <p><a href="https://i.stack.imgur.com/ThQpW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ThQpW.png" alt="enter image description here"></a></p>
  
  <p>and the logs shows following:</p>
  
  <pre><code>2019-11-08 00:36:10.797 ERROR 21904 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [86e57f7e] 500 Server Error for HTTP POST "/product-service/file/upload"
  
  reactor.netty.http.client.PrematureCloseException: Connection has been closed BEFORE response, while sending request body
  </code></pre>
  
  <p><strong>Note</strong> that the gateway is configured to take in large file size with <code>RequestSize</code> filter set globally to take way more than 10MB.</p>
  
  <p><strong>How can I get the same response code as given by the downstream service?</strong></p>
  
  <p>Also, I check with traditional Zuul, and i get a <code>500</code> error too.</p>
  
  <p><strong>For the gateway</strong>, for this particular case I know we can use the <code>RequestSize</code> filter and now the gateway will return the error code, but then we have to identify all the routes that expect this beforehand.
  <strong>Also, other validation in the API, like authorization, etc will have the same the same issue. The response code produced because of these validations will not propagate up</strong>. </p>
  
  <p>Sample code spring-cloud-gateway/product-service/eureka - <a href="https://github.com/dhananjay12/spring-cloud/tree/master/spring-routing" rel="nofollow noreferrer">https://github.com/dhananjay12/spring-cloud/tree/master/spring-routing</a></p>