Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:26:52+00:00 2026-06-10T06:26:52+00:00

Recently I was using RestSharp to consume my Restful Resouce. and expected exchanging data

  • 0

Recently I was using RestSharp to consume my Restful Resouce. and expected exchanging data with JSon between server and client. Below is my C# code.

var client = new RestSharp.RestClient();
var request = new RestRequest(sUrl,Method.POST);
request.RequestFormat = DataFormat.Json;
request.Timeout =  TIME_OUT_MILLISECONTS ;
request.AddHeader("Content-Type", "application/json");
request.AddBody(new { appID = sAppId, loginName = sUserName, password=sPassword });
var response = client.Execute(request);
string s=response.Content;//It is always XML format.

The result is not what I expected for(Json data format), although I had set the RequestFormat Json and add Http header Content-Type. So I decided to use the .Net Reflector to found out What happened in the RestClient.Execute method. Here is the code of the method.

public RestClient()
    {
        ...
        this.AddHandler("application/json", new JsonDeserializer());
        this.AddHandler("application/xml", new XmlDeserializer());
        this.AddHandler("text/json", new JsonDeserializer());
        this.AddHandler("text/x-json", new JsonDeserializer());
        this.AddHandler("text/javascript", new JsonDeserializer());
        this.AddHandler("text/xml", new XmlDeserializer());
        this.AddHandler("*", new XmlDeserializer());
        ...
    }

I have some questions about it:

  1. As the RestClient adds many kinds of Content-Type into the HttpWebRequest. Is it right way to build a Request? And I think Maybe that is the reason why Response.Content always XML.

  2. I don’t know why the RestClient needs to build a HttpWebRequest like that. Any meaning to do that?

  3. If we specified both JSon and XMl message format in a Http Request, which one works finally? Is it allowed?

Thanks. Have a good day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:26:54+00:00Added an answer on June 10, 2026 at 6:26 am

    RestSharp will use the correct handler based on the content type of the response. That’s what those AddHandlers are doing; its configuring the RestClient to accept certain content types in the response and mapping those types to deserializers. Normally you would want to set an accept header for the json content type which notifies the server to send json in the response.

    request.AddHeader("Accept", "application/json")
    

    Of course, this assumes that the server you are hitting is configured to respond with json.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started using a new server for a client and am having problems
I recently started using jQuery/AJAX for submitting data to my PHP API. Currently what
I recently started using lcov to visualize my code coverage. It's a great tool.
I recently started using SourceMonitor to review my (and others) code. Though I understand
I recently was using HttpWebResponse to return xml data from a HttpWebRequest, and I
I am recently using JSON to store configuration parameters for a certain number of
Recently when using the DirectionsService with waypoints between the origin and destination locations for
I've recently been using array_map to replace this kind of code: $users = ...;
Was recently using some code along the lines of $(#divMenuContainer:visible).hide(explode); However after some time
I am recently using gcov to collect the code coverage info. gcov plays well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.