0

I have a web application written in ASP.NET. All is working okay, except that I would like to compress the data being returned. The data is basically a List of custom models. Currently I do something like:

string json_string = new JavaScriptSerializer().Serialize(my_models);

using (var output = new MemoryStream())
{
    using (var compressor = new Ionic.Zlib.GZipStream(output, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression))
    {
            compressor.Write(Encoding.ASCII.GetBytes(json_string), 0, json_string.Length);
    }
}

I then proceed with the following:

HttpResponseMessage json = Request.CreateResponse(HttpStatusCode.OK, json_string);

json.Content.Headers.Add("content-encoding", "gzip");

This causes an application error. In Chrome (through the console), I see the following message:

ERR_CONTENT_DECODING_FAILED

Where am I going wrong here? Thanks!

Just Rudy
  • 700
  • 11
  • 28
  • Here is a related post, maybe this helps: [ERR_CONTENT_DECODING_FAILED](http://stackoverflow.com/questions/14039804/error-330-neterr-content-decoding-failed) – Robert Green MBA Sep 14 '16 at 20:28
  • What exactly is the issue, can you show a stack trace or something with a bit more details... The error in chrome is pretty vague, but it does tell you the decoding has failed, with that being said, the more details your provide, the better I can help. – TGarrett Sep 14 '16 at 20:30
  • I don't know if this is the cause of your error, but your code doesn't actually do anything with the compressed bytes(`output`). It kinda just falls out of scope – Sam I am says Reinstate Monica Sep 14 '16 at 20:39

1 Answers1

0

So in your code you wrote the compressed content to your MemoryStream but your json_string is still your original json_string which then you added that original string as response but marked it as compressed gzip format.

So in the end Chrome tries to decode a pure string JSON content with gzip and it failed to do so

You would want to write whats inside output into json_string object before sending it off

Or you can config IIS to always compress everything to save yourself some trouble.

Steve
  • 11,696
  • 7
  • 43
  • 81