marc_s
marc_s

Reputation: 754398

Telling RestSharp *not* to add a specific HTTP header

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?

Upvotes: 1

Views: 2660

Answers (1)

Aleksandr Ivanov
Aleksandr Ivanov

Reputation: 2786

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.

Upvotes: 3

Related Questions