Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI Gzip when returning HttpResponseMessage

I have a WebAPI controller that returns an HttpResponseMessage and I want to add gzip compression. This is the server 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); } 

And this is the client code for the ajax call, using jquery:

$.ajax({     url: "/SomeRoute",     type: "POST",     cache: "false",     data: SomeData,     beforeSend: function (jqXHR) { jqXHR.setRequestHeader('Accept-Encoding', 'gzip'); },     success: function(msg) { ... } 

When I run this, the server code returns without bugging but the client bugs:

(failed) net::ERR_CONTENT_DECODING_FAILED 

enter image description here

When I look with Fiddler, this is what I see:

enter image description here

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:

enter image description here

Please note that I'm not looking for an IIS setting because I don't have access to that (hosting).

like image 871
frenchie Avatar asked Jul 18 '14 00:07

frenchie


People also ask

How do I use GZip compression in web API?

Then use the following class at the method level as in the following: . Execute the Web API method by commenting the DeflateCompression attribute that is applied on the Web API controller method, you will find that returned data is not compressed but simple JSON. See the execution time when compression is not applied.

Does ASP NET core support response compression?

When you're unable to use the compression features of web servers (IIS, Apache, Nginx), ASP.NET Core provides an alternate option, Response Compression middleware.

What is spring boot compression?

Enabling GZip compression in Spring Boot GZip compression is a very simple and effective way to save bandwidth and improve the speed of your website. It reduces the response time of your website by compressing the resources and then sending it over to the clients. It saves bandwidth by at least 50%.

What is server compression enabled?

server.compression.enabled= true. # Minimum response where compression will kick in. server.compression.min-response-size= 4096. # Mime types that should be compressed. server.compression.mime-types=text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json.


1 Answers

Add these NuGet packages:

Microsoft.AspNet.WebApi.Extensions.Compression.Server System.Net.Http.Extensions.Compression.Client

Then and add one line of code to App_Start\WebApiConfig.cs:

GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor())); 

That will do the trick!

Details at:

  • NuGet package page
  • GitHub

Hope that helps.

**Updated after comment from @JCisar

Update for ASP.Net Core

Nuget Package is

Microsoft.AspNetCore.ResponseCompression

like image 167
Pooran Avatar answered Sep 23 '22 01:09

Pooran