Base solution for your next web application
Open Closed

Compression of POST request #3874


User avatar
0
jeromevoxteneo created

Hello,

For performance reason, we would like to compress the content of the Post request on a mobile app that call our api.

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


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    I suppose you should add your decompression filter before ABP's ValidationFilter. Did you try to add [DisableValidation] to your action to understand if it's actually related to the validation. Also, can you share the exception message and stack trace in the logs.

  • User Avatar
    0
    strix20 created

    I would think you would also want to define a custom model binder, as well, so you can unzip during model binding, instead of having to messily accessing the post body from a request event.

  • User Avatar
    0
    jeromevoxteneo created

    Thanks for your responses.

    I finally make it working by following this answer: <a class="postlink" href="https://stackoverflow.com/questions/42075792/net-web-api-2-post-consuming-gzip-compressed-content/46282062#comment79550906_46282062">https://stackoverflow.com/questions/420 ... 6_46282062</a>

    Implemented a delegate.

    Result: Json from my mobile application is compressed and I add a header in the Content "gzip" On the Web the content is decompressed if the header is present.

    Half of the size for my big call ! :)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @JeromeVoxTeneo for sharing your solution.