user3801443
user3801443

Reputation: 61

restsharp accept-encoding disabling compression

In a particular case I need to be able to disable compression in the requst/response.

Using Firefox RestClient I am able to post some xml to a web service and get some response xml successfully with a single header parameter "Accept-Encoding" : " " which if I do not set this header, the response body would come back compressed with some binary data in the response body(that's why I want to disable gzip in response)

Now using the same header value in my app (using RestSharp in C#), I still get the binary data (gzip) in response.

Can someone please shed some light? Is it supported in RestSharp?

Upvotes: 6

Views: 5910

Answers (5)

wancholo
wancholo

Reputation: 11

As of RestSharp version 107, the AddDecompressionMethod has been removed and most of the client options has been move to RestClientOptions. Posting here the solution that worked for me, in case anyone needs it.

var options = new RestClientOptions(url)
{
    AutomaticDecompression = DecompressionMethods.None
};

_client = new RestClient(options);

Upvotes: 1

Maoz
Maoz

Reputation: 311

Not sure if this relevant anymore, but for maybe future references

RestRequest has IList<DecompressionMethods> AllowedDecompressionMethods, and when creating new RestRequest the list is empty. Only when calling the Execute method it fills with the default values (None, Deflate, and GZip) unless it's not empty

To update the wanted decompression method, simply use the method named AddDecompressionMethod and add the wanted decompression method - and that's that

Example:

var client = new RestClient();
var request = new RestRequest(URL, Method.GET, DataFormat.None);
request.AddDecompressionMethod(DecompressionMethods.GZip);
var response = client.Execute(request);

Upvotes: 1

Carl Heinrich Hancke
Carl Heinrich Hancke

Reputation: 2810

Using RestSharp v106.11.4, I was unable to turn off automatic decompression as Bo Ngoh suggested. I set the AutomaticDecompression on the RestClient instance at the moment it gets instantiated, but still the Accept-Encoding header was added.

The way to set this & disable the decompression is through the ConfigureWebRequest method, which is exposed on the RestClient. Below snippet allowed me to turn off this feature:

var client = new RestClient();
client.ConfigureWebRequest(wr =>
{
    wr.AutomaticDecompression = DecompressionMethods.None;
});

Upvotes: 2

Bo Ngoh
Bo Ngoh

Reputation: 163

The feature (only just) seems to have been added, but stealthily - without a note on the issue's status nor on the changelogs. Possibly as it hasn't been sufficiently tested?

Nevertheless I recently had a need for this functionality and tested it - and it works. Just set the RestClient instance's AutomaticDecompression property to false.

If you intend to keep your RestClient instance long-lived remember to do this before its first use - the setting seems to be 'locked in' after use and cannot change after. In my case I needed to make calls with and without AutomaticDecompression so i simply created two different RestClient instances.

Upvotes: 2

wal
wal

Reputation: 17749

RestSharp does not support disabling compression.

If you look at the source code in Http.Sync.cs line 267 (assuming a sync request, async has the same code duplicated in Http.Async.cs line 424)

webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;

that is, the underlying WebRequest that Restsharp uses to make the http call has the compression options hardcoded. There is an open issue that documents this

Upvotes: 2

Related Questions