title: Spring Boot Web Client pdf binary empty first time when using async subscribe
body: <p>Can't figure out how to obtain response body and send back to client properly.  It has an empty body on first execution and works thereafter.  Looks like am not handling the async nature of subscribe properly.</p>
  <p>Have a controller and service as follows:</p>
  <pre><code>ResponseEntity&lt;StreamingResponseBody&gt; responseEntity = null;
  
  @GetMapping(value = &quot;/getPdf&quot;, produces = MediaType.APPLICATION_PDF_VALUE)
  public ResponseEntity&lt;StreamingResponseBody&gt; getDoc(...
  
  responseEntity = docervice.getByDocIdResponseEntity(userName, docIdTst);
  </code></pre>
  <p>Service is:</p>
  <pre><code>public ResponseEntity&lt;StreamingResponseBody&gt; getByDocIdResponseEntity(String userName, String docId) {
  this.webClient
      .get()
      .uri(uriBuilder -&gt; uriBuilder.path(this.getByDocIdBaseURL)
          .queryParam(&quot;user&quot;, userName).queryParam(&quot;docId&quot;, docId)
          .build())
      .retrieve()       
      .toEntity(byte[].class)
      .subscribe(doc -&gt; {
          log.info(&quot;getContentLength: &quot; + doc.getHeaders().getContentLength());
          log.info(&quot;content type: &quot; + doc.getHeaders().getContentType().getType() + &quot;/&quot; + doc.getHeaders().getContentType().getSubtype());
  
          // Extract filename
          String disposition = doc.getHeaders().getFirst(&quot;Content-Disposition&quot;);
          String filename = null;
          if (disposition != null) {
              int fileSplit = disposition.indexOf(&quot;=&quot;);
              if (fileSplit &gt; 0) {
                  filename = disposition.substring(fileSplit + 1);
                  log.debug(&quot;filename: &quot; + filename);
              }
          }
  
          InputStream targetStream = new ByteArrayInputStream(doc.getBody());
  
          // stream the response by writing to OutputStream
          StreamingResponseBody responseBody = outputStream -&gt; {
              int nRead;
              byte[] data = new byte[config.getDefaultBufferSize()];
              while ((nRead = targetStream.read(data, 0, data.length)) != -1) {
                  outputStream.write(data, 0, nRead);
              }
          };
  
          // response to client
          docIdResponseEntity = ResponseEntity.ok()
              .header(HttpHeaders.CONTENT_DISPOSITION, &quot;inline; filename=&quot; + filename)
              .contentType(MediaType.APPLICATION_PDF).body(responseBody);
      });
  
  return docIdResponseEntity;
  }
  </code></pre>
  <p>How do I handle the async response and send back the ResponseEntity to consumer once ready?  Am reading the docs but can't see how to do it.</p>