dibs487
dibs487

Reputation: 1364

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.

Upvotes: 9

Views: 12673

Answers (2)

richzilla
richzilla

Reputation: 41992

The ContentEncoding property is an instance of ICollection.

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

Upvotes: 11

dibs487
dibs487

Reputation: 1364

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");

Upvotes: 6

Related Questions