Why is RestSharp AddHeader("Accept", "application/json"); = to a list of item?

13,651

From https://groups.google.com/forum/#!topic/restsharp/KD2lsaTC0eM:

The Accept header is automatically generated by inspecting the "handlers" that are registered with the RestClient instance. One optin is to use RestClient.ClearHandlers (); and then add back explicitly the JSON deserializer with RestClient.AddHandler("application/json", new JsonDeserializer ());

Share:
13,651

Related videos on Youtube

user2408952
Author by

user2408952

Updated on June 12, 2022

Comments

  • user2408952
    user2408952 almost 2 years

    I've got this Windows Phone 8 project in C#, where I'm using RestSharp 104.4.0 to make a request to a server. The server only accepts a "Accept" type of "application/json". My code to call the request:

    var client = new RestClient(_jsonBaseUrl + someURL)
    {
        Authenticator = new HttpBasicAuthenticator(someUsername, somePassword)
    };
    
    var request = new RestRequest(Method.POST);
    
    UTF8Encoding utf8 = new UTF8Encoding();
    byte[] bytes = utf8.GetBytes(json);
    json = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
    
    request.RequestFormat = DataFormat.Json;
    request.AddHeader("Accept", "application/json");
    request.AddBody(json);
    request.Parameters.Clear();
    request.AddParameter("application/json", json, ParameterType.RequestBody);
    
    client.ExecuteAsync<UserAccount>(request, response =>
    {
        if (response.ResponseStatus == ResponseStatus.Error)
        {
            failure(response.ErrorMessage);
        }
        else
        {
            success("done");
        }
    });
    

    the "json" variable is a JSON string.

    As you can see i've set my accept type to AddHeader("Accept", "application/json"); But for some wired reason the server receives this as accept type: "Accept": "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml"

    What do i have to do, to make sure the only accept type the server gets is "Accept": "application/json"?

    Any kind of help would be much appreciated.

  • user2408952
    user2408952 about 10 years
    Have no idea why i couldn't find that with all my Google searches:) but thanks ill give it a shot.
  • Toni Petrina
    Toni Petrina about 10 years
    It was practically the first result :D
  • user2408952
    user2408952 about 10 years
    it works, RestClient.ClearHandlers (); then RestClient.AddHandler("application/json", new JsonDeserializer ()); then delete request.AddHeader("Accept", "application/json");