title: Decode content-encoding gzip using Spring WebClient
body: <p>I am calling a web service using Spring WebClient (Spring 5.1.3). The service responds with <code>content-type: application/json</code> and <code>content-encoding: gzip</code></p>
  
  <p><code>ClientResponse.bodyToMono</code> then fails with the error "JSON decoding error: Illegal character ((CTRL-CHAR, code 31))" which I assume is because the content has not been decoded before trying to parse the JSON.</p>
  
  <p>Here is code snippet (simplified) of how I create the WebClient</p>
  
  <pre><code>HttpClient httpClient = HttpClient.create().secure(sslContextSpec -&gt; sslContextSpec.sslContext(sslContext));
  return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
  </code></pre>
  
  <p>I then use the WebClient to make the call:</p>
  
  <pre><code>webClient.get().uri(uri)
      .accept(MediaType.APPLICATION_JSON)
      .header(HttpHeaders.ACCEPT_ENCODING, "gzip")
      .exchange()
  </code></pre>
  
  <p>The HTTP request has 2 headers:</p>
  
  <pre><code>Accept: application/json
  Accept-Encoding: gzip
  </code></pre>
  
  <p>The response has the following headers:</p>
  
  <pre><code>set-cookie: xxx
  content-type: application/json; charset=utf-8
  content-length: 1175
  content-encoding: gzip
  cache-control: no-store, no-cache
  </code></pre>
  
  <p>By doing the following I am able to manually decode the GZIP content and get valid JSON from the result</p>
  
  <pre><code>webClient.get().uri(uri)
          .accept(MediaType.APPLICATION_JSON)
          .header("accept-encoding", "gzip")
          .exchange()
          .flatMap(encodedResponse -&gt; encodedResponse.body((inputMessage, context) -&gt;
                  inputMessage.getBody().flatMap(dataBuffer -&gt; {
                      ClientResponse.Builder decodedResponse = ClientResponse.from(encodedResponse);
                      try {
                          GZIPInputStream gz = new GZIPInputStream(dataBuffer.asInputStream());
                          decodedResponse.body(new String(gz.readAllBytes()));
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      decodedResponse.headers(headers -&gt; {
                          headers.remove("content-encoding");
                      });
                      return Mono.just(decodedResponse.build());
                  }).flatMap(clientResponse -&gt; clientResponse.bodyToMono(Map.class))
  </code></pre>