Compression is not new and not mandatory but is one of the main point you can work on if you want to optimize your process.
By reducing the size of the data (Up to 8 times smaller), your process will be faster to send and receive data and you will preserve your bandwidth.
The negative impact is that it will use CPU but the difference is so small that the advantages of compression take the ownership.
The API supports two types of compression:
GZIP
or deflate
)GZIP
or deflate
)
You can receive a compressed response from the API with reduced size.
It will make your api calls REALLY faster. And it isn't difficult to de-compress at your end.
.NET and Web browser will do it for you automatically (or with one line of code).
All you need to do to request compressed content is to add Accept-Encoding: gzip
on the HTTP Request header, the API will take care of the rest.
If your client cannot handle GZIP
you can use deflate
. Use the following HTTP Request header Accept-Encoding: deflate
GZIP will compress the response payload to save size. By reducing the payload size request-reponse time will be reduced.
The next question which comes to your mind is how do I decompress the response.
When using a .NET client
You just need to set AutoCompression
to GZip and .NET takes care of the rest
HttpWebRequest http = (HttpWebRequest)WebRequest.Create("url"); http.AutomaticDecompression = DecompressionMethods.GZip;
When using API inside of a javascript client
Your web browser will handle the de-compress of GZIP.
API is not only sending you data. You also send data when adding (POST) or updating entities (PUT).
There too you can optimize the speed of the process by sending compressed request body.
You need to tell API that you're sending a compressed request by adding,
For GZIP :Content-Encoding: gzip
.
For Deflate :Content-Encoding: deflate
.
You will need to manually compress the request body before sending to API.
Compressing using a .NET
using (HttpClientHandler handler = new HttpClientHandler()) { handler.AutomaticDecompression = compression; using (HttpClient client = new HttpClient(handler, false)) { string token = "Your authentication token" client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token); byte[] jsonBytes = Encoding.UTF8.GetBytes(body); MemoryStream ms = new MemoryStream(); if (compression == DecompressionMethods.GZip) { using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true)) { gzip.Write(jsonBytes, 0, jsonBytes.Length); } } else { using (DeflateStream deflate = new DeflateStream(ms, CompressionMode.Compress, true)) { deflate.Write(jsonBytes, 0, jsonBytes.Length); } } ms.Position = 0; StreamContent content = new StreamContent(ms); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); content.Headers.ContentEncoding.Add(encoding); HttpResponseMessage res = await client.PostAsync(_url + resource, content); response = await res.Content.ReadAsStringAsync(); } }
Compressing using a javascript on client side
You must rely on third party javascript libraries which supports either GZIP
or deflate
.