In this post we are going to see how to create a compressed web api response to user, because of compressing the result content-length will be less and it is more faster in get response. First let we create a new Web Api project.
From the above code we can see the student list is returning, now we test this in post man rest client.
from the above , you can see the result is content-length is 219, now we will enable the GZip compression. For this we have to create actionFilterAttribute there we have to set the content-encoding as gzip. we have to add a package DotnetZip from nuget package manager
Finally we have to add this action filter above the api method
Now we see the response in post man client, content-length will be reduced.
From this post you can learn how to create a compressing web api response using Gzip.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string
Department { get; set; }
public int Marks { get; set; }
}
public IEnumerable<Student> Get()
{
List<Student> studs = new List<Student>();
studs.Add(new Student() { Id = 1, Name = "rajesh",
Department ="CSE", Marks=83});
studs.Add(new Student() { Id = 2, Name = "suresh",
Department = "ECE", Marks=92 });
studs.Add(new Student() { Id = 2, Name = "ramesh",
Department = "CSE", Marks = 76 });
studs.Add(new Student() { Id = 2, Name = "ramu",
Department = "ECE", Marks = 94 });
return studs;
}
From the above code we can see the student list is returning, now we test this in post man rest client.
from the above , you can see the result is content-length is 219, now we will enable the GZip compression. For this we have to create actionFilterAttribute there we have to set the content-encoding as gzip. we have to add a package DotnetZip from nuget package manager
public class GZipCompression
{
public static byte[] GzipByte(byte[] input)
{
if (input == null)
return null;
using (var result= new MemoryStream())
{
using(var compress
= new Ionic.Zlib.GZipStream(result,
Ionic.Zlib.CompressionMode.Compress))
{
compress.Write(input, 0,
input.Length);
}
return result.ToArray();
}
}
}
public class ZipCompressionAttribute:ActionFilterAttribute
{
public override void
OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var content = actionExecutedContext.Response.Content;
var bytes = content == null ? null :
content.ReadAsByteArrayAsync().Result;
var compressedContent = bytes == null ? new byte[0] : GZipCompression.GzipByte(bytes);
actionExecutedContext.Response.Content = new ByteArrayContent(compressedContent);
actionExecutedContext.Response.Content.Headers.Remove("Content-Type");
actionExecutedContext.Response.Content.Headers.Add("Content-encoding", "gzip");
actionExecutedContext.Response.Content.Headers.Add("Content-Type", "application/json");
base.OnActionExecuted(actionExecutedContext);
}
}
Finally we have to add this action filter above the api method
[ZipCompression]
public IEnumerable<Student> Get()
{
List<Student> studs = new List<Student>();
studs.Add(new Student() { Id = 1, Name = "rajesh",
Department ="CSE", Marks=83});
studs.Add(new Student() { Id = 2, Name = "suresh",
Department = "ECE", Marks=92 });
studs.Add(new Student() { Id = 2, Name = "ramesh",
Department = "CSE", Marks = 76 });
studs.Add(new Student() { Id = 2, Name = "ramu",
Department = "ECE", Marks = 94 });
return studs;
}
Now we see the response in post man client, content-length will be reduced.
From this post you can learn how to create a compressing web api response using Gzip.
React.JS online training in india
ReplyDeletePython online Training in India
Oracle DBA online training in India
Java online Training in India
SAP online Training In india
Its really nice and informative.. Thanks for sharing
ReplyDeleteBest Web Design Training Institutes in Noida
Best Hadoop Training Institutes In Noida
Best Digital Marketing Training Institute in Noida
Sap Training Institute in Noida
Best Java Training Institute in Noida
SAP SD Training Institute in Noida
Best Auto CAD Training Institute In Noida
Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.
ReplyDeleteAutoCAD training in Noida
AutoCAD training institute in Noida
Best AutoCAD training institute in Noida
Cloud Computing Training In Noida
ReplyDeleteWebtrackker is IT based company in many countries. Webtrackker will provide you a real time projects based training on Cloud Computing. If you are looking for the Cloud computing training in Noida then you can join the webtrackker technology.
Cloud Computing Training In Noida , Cloud Computing Training center In Noida , Cloud Computing Training institute In Noida ,
Company Address:
Webtrackker Technology
C- 67, Sector- 63, Noida
Email: info@webtrackker.com
Website: www.webtrackker.com
http://webtrackker.com/Cloud-Computing-Training-Institutes-In-Noida.php
ReplyDeleterpa training institute in noida
Blockchain training institute in Noida
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
ReplyDeleteCEH Training In Hyderbad
Awesome post...
ReplyDeleteinternship report on python
free internship in chennai for ece students
free internship for bca
internship for computer science engineering students in india
internships in hyderabad for cse students 2018
electrical companies in hyderabad for internship
internships in chennai for cse students 2019
internships for ece students
inplant training in tcs chennai
internship at chennai
I’m really happy to say it was an interesting post to read. I learned new information from your article. You are doing a great job. Thanks for sharing as a wonderful information..
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training