Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the content encoding on a HttpResponseMessage

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.

like image 343
dibs487 Avatar asked Feb 06 '17 16:02

dibs487


People also ask

What is HTTP content encoding?

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.

Where do I put accept-encoding?

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.

What is content encoding gzip?

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.


2 Answers

The ContentEncoding property is an instance of ICollection.

This provides .Add() and .Clear() methods for controlling the contents.

like image 117
richzilla Avatar answered Oct 27 '22 10:10

richzilla


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");
like image 34
dibs487 Avatar answered Oct 27 '22 10:10

dibs487