1
votes

I am trying to call a REST service from a C# ASP.NET 4.0 application using RestSharp.

It's a fairly straightforward POST call to a https:// address; my code is something like this (CheckStatusRequest is a plain simple DTO with about four or five string and int properties - nothing fancy):

public CheckStatusResponse CheckStatus(CheckStatusRequest request) {
    // set up RestClient
    RestClient client = new RestClient();
    string uri = "https://.......";

    // create the request (see below)
    IRestRequest restRequest = CreateRequestWithHeaders(url, Method.POST);

    // add the body to the request
    restRequest.AddBody(request);

    // execute call
    var restResponse = _restClient.Execute<CheckStatusResponse>(restRequest);
}   

// set up request
private IRestRequest CreateRequestWithHeaders(string uri, Method method) {
    // define request
    RestRequest request = new RestRequest(uri, method);

    // add two required HTTP headers
    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-Type", "application/json");

    // define JSON as my format
    request.RequestFormat = DataFormat.Json;

    // attach the JSON.NET serializer for RestSharp
    request.JsonSerializer = new RestSharpJsonNetSerializer();

    return request;
}

The problem I'm having when I send these requests through Fiddler to see what's going on is that my request suddenly gets a third and unwanted HTTP header:

POST https://-some-url- HTTP/1.1
Accept: application/json
User-Agent: RestSharp/104.4.0.0
Content-Type: application/json
Host: **********.com
Content-Length: 226
Accept-Encoding: gzip, deflate   <<<=== This one here is UNWANTED!
Connection: Keep-Alive

I suddenly have that Accept-Encoding HTTP header, which I never specified (and which I don't want to have in there). And now my response is no longer proper JSON (which I'm able to parse), but suddenly I get back gzipped binary data instead (which doesn't do real well when trying to JSON-deserialize)....

How can I get rid of that third unwanted HTTP header?

  • I tried to set it to something else - whatever I enter just gets appended to those settings
  • I tried to somehow "clear" that HTTP header - without any success
  • I tried finding a property on the RestClient or the RestRequest classes to specify "do not use GZip"
1

1 Answers

3
votes

Looking at the sources (Http.Sync.cs and Http.Async.cs) of RestSharp you can see that these values are hardcoded:

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

There is also an open issue that describes this problem. It was opened August 2014 but still not solved. I think you can leave a comment there and maybe they will pay attention.