Decompressing Traffic for HTTP Responses

Why Is it Important to Set Up Your Projects for Decompression?

iMIS is currently configuring its environment to support compression for iMIS with IIS. Due to this change, there may be projects or other applications that are performing compression by default without the developer or user being aware of it. With this shift in the environment surrounding iMIS EMS, you might encounter projects that appear to break due to encoding issues. This could be a decompression problem on the part of the project, as it might not know how to interpret the response coming back from the iMIS API.

To assist you in navigating this change, we have provided resources for both C# and JavaScript below. These resources can help you prepare your projects to handle and interpret the compressed HTTP response.

C#

If you're working with C#, here's a quick guide on preparing your projects for compressed traffic through HTTP.

Using the ConfigureServices function in your startup class.

In C#, a shared module can be utilized to decompress traffic from a service to a web app. The following code snippet demonstrates how to configure an HTTP client for automatic decompression:

services.AddHttpClient("nameofclient", client =>
            {
                client.BaseAddress = new Uri("http://www.httpbin.org");
                client.DefaultRequestHeaders.Add("Accept", "application/xml");
            })
 .ConfigurePrimaryHttpMessageHandler(messageHandler =>
            {
                var handler = new HttpClientHandler();

                if (handler.SupportsAutomaticDecompression)
                {
                   handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                }
                return handler;
            });

For more details and examples, you can refer to Add HTTP Compression to your HttpClient.

Alternative Decompression Class

If you prefer an alternative method of decompressing in C# you can use the following class:

public static byte[] Decompress(byte[] bytes)
{
    using (var memoryStream = new MemoryStream(bytes))
    using (var outputStream = new MemoryStream())
    using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress))
    {
        decompressStream.CopyTo(outputStream);
        return outputStream.ToArray();
    }
}

 

For more details and examples, you can refer to How to compress and decompress strings in C#.

JavaScript

For JavaScript users working with Node.js, Ajax, or potentially RxJs projects, here's a guide on handling compressed traffic.

Browser Decompression

There is no need to worry about browser compatibility, as modern browsers automatically decompress any traffic sent to them.

JavaScript API for Decompression

Gzip:

const compressedReadableStream = stream.pipeThrough(
  new DecompressionStream("gzip")
);

 

Deflate:

const compressedReadableStream = stream.pipeThrough(
  new DecompressionStream("deflate")
);

 

For more details and examples, you can refer to JSON compression in the browser, with gzip and the Compression Streams API..

Feel free to choose the method that best fits your project's needs!