title: WebAPI Gzip when returning HttpResponseMessage
body: <p>I have a WebAPI controller that returns an <code>HttpResponseMessage</code> and I want to add gzip compression. This is the server code:</p>
  
  <pre><code>using System.Net.Http;
  using System.Web.Http;
  using System.Web;
  using System.IO.Compression;
  
  [Route("SomeRoute")]
  public HttpResponseMessage Post([FromBody] string value)
  {
      HttpContext context = HttpContext.Current;
  
      context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
  
      HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
      HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;
  
      return new SomeClass().SomeRequest(value);
  }
  </code></pre>
  
  <p>And this is the client code for the ajax call, using jquery:</p>
  
  <pre><code>$.ajax({
      url: "/SomeRoute",
      type: "POST",
      cache: "false",
      data: SomeData,
      beforeSend: function (jqXHR) { jqXHR.setRequestHeader('Accept-Encoding', 'gzip'); },
      success: function(msg) { ... }
  </code></pre>
  
  <p>When I run this, the server code returns without bugging but the client bugs: </p>
  
  <pre><code>(failed)
  net::ERR_CONTENT_DECODING_FAILED
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/YeKe3.png" alt="enter image description here"></p>
  
  <p>When I look with Fiddler, this is what I see:</p>
  
  <p><img src="https://i.stack.imgur.com/7ZyTz.png" alt="enter image description here"></p>
  
  <p>What do I need to change to make the web service return gzipped content that the client processes normally? I know I could also do this with an HttpModule or through some setting on IIS but neither option fits the scenario of the hosting:</p>
  
  <p><img src="https://i.stack.imgur.com/jnLwU.png" alt="enter image description here"></p>
  
  <p><strong>Please note that I'm not looking for an IIS setting because I don't have access to that (hosting).</strong></p>