How to accept compressed post request data in ASP.NET Boilerplate

521 Views Asked by At

We have a xamarin mobile app requesting and sending huge data to an Asp.net Boilerplate web back-end.

The response are compressed using gzip, and we would like to compress the content of the Post request also.

To do so, we compress the json serialization of the parameter of the post method as following:

byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
MemoryStream ms = new MemoryStream();
using (GZipStream gzipStream = new GZipStream(ms, CompressionMode.Compress, true))
{
    gzipStream.Write(jsonBytes, 0, jsonBytes.Length);                   
}
ms.Position = 0;
StreamContent content = new StreamContent(ms);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
content.Headers.ContentEncoding.Add("gzip");

In response of the Post, we receive an error 500 with an Abp Validation error has content.

How could we configure or override the server to accept the gzip content ?

We have tried with a module to add, at Context_BeginRequest, a filter to decompress the content if the it is gzip has following, but without result. (The filter is applied, but it doesn’t help)

string requestEncoding = ctx.Request.Headers["Content-encoding"];
if (requestEncoding != null && requestEncoding == "gzip")
{
    app.Request.Filter = 
         new System.IO.Compression.GZipStream(app.Request.Filter,
                                              CompressionMode.Decompress);
}

Thanks

0

There are 0 best solutions below