I want to set the Content-Encoding on a HttpResponseMessage and I can't for the life of me find out how. Given this WebApi action
public HttpResponseMessage Get()
{
byte[] tile = GetTile();
var result = new HttpResponseMessage(HttpStatusCode.OK) {Content = new ByteArrayContent(tile)};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");
return result;
}
How do I set the Content-Encoding on the header to gzip?
result.Content.Headers.ContentEncoding
is read only.
Content encoding is mainly used to compress the message data without losing information about the origin media type. Note that the original media/content type is specified in the Content-Type header, and that the Content-Encoding applies to the representation, or "coded form", of the data.
To check this Accept-Encoding in action go to Inspect Element -> Network check the request header for Accept-Encoding like below, Accept-Encoding is highlighted you can see.
Gzip is a file format and software application used on Unix and Unix-like systems to compress HTTP content before it's served to a client.
The ContentEncoding
property is an instance of ICollection.
This provides .Add()
and .Clear()
methods for controlling the contents.
Not to detract from richzilla's answer which is of course totally correct and answered my question. Seeing as this is getting a few votes and visits there must be other people making the same mistake I did so it's worth saying the complete answer to my problem was
result.Content.Headers.ContentEncoding.Add("gzip");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With