# ASP.NET Core: support compressed content

> In the previous article, We've seen how to enable response compression, and in this article we're going to see how to accept requests that have compressed content.…

- Author: Jérôme Giacomini
- Language: en
- Canonical URL: [ASP.NET Core: support compressed content](https://jeromegiacomini.net/articles/2018/09/25/asp-net-core-support-compressed-content)
- Published: 2018-09-25
- Last modified: 2026-09-05
- Topics: ASP.NET Core, .NET, C#

[In the previous article,](https://jeromegiacomini.net/Blog/2018/09/25/asp-net-core-activer-la-compression-des-reponse-en-gzip-deflate-brotli/) We've seen how to enable response compression, and in this article we're going to see how to accept requests that have compressed content.

ASP.NET Core doesn't provide any built-in classes to support this scenario, but fortunately it's flexible enough to add this behavior in a few lines.

The idea is to decompress the body of requests that have as head **Content encoding** with values **gzip**, **decay** or **br**.

```csharp

    public class CompressionMiddleware
    {
        private readonly RequestDelegate _next;

        public CompressionMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            // Récupération de l'entête Content-Encoding.
            var content = context.Request.Headers["Content-Encoding"];
            if (!string.IsNullOrWhiteSpace(content) &amp;&amp; context.Request.Body != null)
            {
                if (content.Contains("gzip"))
                {
                    // On remplace le stream par un stream Gzip
                    context.Request.Body = new GZipStream(context.Request.Body, CompressionMode.Decompress);
                }
                else if (content.Contains("br"))
                {
                    // On remplace le stream par un stream Brotli
                    context.Request.Body = new BrotliStream(context.Request.Body, CompressionMode.Decompress);
                }
                else if (content.Contains("deflate"))
                {
                    // On remplace le stream par un stream deflate
                    context.Request.Body = new DeflateStream(context.Request.Body, CompressionMode.Decompress);
                }
            }

            await _next(context);
        }
    }

```


Once the middleware is defined, it must be added to the method **Configuring** of the class **Start-up**

```csharp

            app.UseMiddleware&lt;CompressionMiddleware&gt;();

```


Happy coding !
