title: Correct way to compress webapi POST
body: <p>I have a webform page that calls a webapi method to save some data. I am using the HttpClient to make the call and execute the webapi. </p>
  
  <p>I tried to use webAPI compression to post a huge xml to the API.
  I basically used these two websites as reference:
  <a href="http://www.ronaldrosier.net/blog/2013/07/16/implement_compression_in_aspnet_web_api" rel="noreferrer">http://www.ronaldrosier.net/blog/2013/07/16/implement_compression_in_aspnet_web_api</a> and <a href="http://benfoster.io/blog/aspnet-web-api-compression" rel="noreferrer">http://benfoster.io/blog/aspnet-web-api-compression</a></p>
  
  <p>The API is working, it is triggering the handler correctly. I am facing some problems trying to compress and post the object, from my webforms on the server side.</p>
  
  <p>Here is the code I tried:</p>
  
  <pre><code>bool Error = false;
  //Object to post. Just an example...
  PostParam testParam = new PostParam()
  {
    inputXML = "&lt;xml&gt;HUGE XML&lt;/xml&gt;",
    ID = 123
  };
  
  try
  {
    using (var client = new HttpClient())
    {
      using (var memStream = new MemoryStream())
      {
         var data = new DataContractJsonSerializer(typeof(PostParam));
         data.WriteObject(memStream, testParam);
         memStream.Position = 0;
         var contentToPost = new StreamContent(this.Compress(memStream));
         contentToPost.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
         contentToPost.Headers.Add("Content-Encoding", "gzip");
         var response = client.PostAsync(new Uri("http://myapi/SAVE"), contentToPost).Result;
  
         var dataReceived = response.EnsureSuccessStatusCode();
  
         dynamic results;
         if (dataReceived.IsSuccessStatusCode)
         {
           results = JsonConvert.DeserializeObject&lt;dynamic&gt;(dataReceived.Content.ReadAsStringAsync().Result);
           try
           {
             this.Error = results.errors.Count == 0;
           }
           catch { }
       }
    }
     }
  }
  catch
  {
    this.Error = true;
  }
  
  
  //Compress stream
  private MemoryStream Compress(MemoryStream ms)
          {
              byte[] buffer = new byte[ms.Length];
              // Use the newly created memory stream for the compressed data.
              GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
              compressedzipStream.Write(buffer, 0, buffer.Length);
              // Close the stream.
              compressedzipStream.Close();
              MemoryStream ms1 = new MemoryStream(buffer);
              return ms1;
          }
  </code></pre>
  
  <p>When I execute the code above, it does not throw any error and in the handler, the request.Content.ReadAsStringAsync().result a huge \0\0\0\0\0\0...</p>
  
  <p>Please, can you guys show me what I am doing wrong? How to send the compressed object with the XML to the API correctly?</p>
  
  <p>Thanks </p>