16

I'm creating a RestSharp.RestRequest via:

RestRequest request = new RestRequest();
request.Method = Method.POST;
request.Resource = "/rest-uri";

request.AddHeader("Content-Type", "application/someContentType");

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + Environment.NewLine +
             "<register-request">" + Environment.NewLine +
             "    <name=\"someName\"/>" + Environment.NewLine +
             "</register-request>");

request.AddParameter("text/xml", registerSinkRequest, ParameterType.RequestBody);

(The Content-Type is manually set to application/someContentType)

In debug-mode it also shows Content-Type=application/someContentType

But executing the RestRequest returns an 415 Media Not Supported-Error and WireShark shows that the Media-Type is set to text/xml (like set in the AddParameter-Method).

Why is RestSharp showing a different Content-Type then WireShark? And how can I prevent the Content-Type to be changed (if it is)?

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
DIF
  • 2,470
  • 6
  • 35
  • 49

2 Answers2

20

svick's comment is right. Set the content type in the first parameter of AddParameter() and you can leave out the AddHeader() call.

While that's the 'right' answer, I'll explain why it has a confusing method for doing this that's not exactly obvious.

The intended way to accomplish this is to use AddBody() along with RestRequest.RequestFormat. An example:

var client = new RestClient();
// client.XmlSerializer = new XmlSerializer(); // default
// client.XmlSerializer = new SuperXmlSerializer(); // can override with any implementaiton of ISerializer

var request = new RestRequest();
request.RequestFormat = DataFormat.Xml;
request.AddBody(objectToSerialize);

The serialization of objectToSerialize is based on the registered XmlSerializer. If you use RequestFormat = DataFormat.Json, then the RestClient.JsonSerializer is used. Implementations of ISerializer (which you can use to override the default serialization) declare their own Content-Types which is what gets passed through the janky AddParameter() overload you're using.

AddParameter(contentType, content, ParameterType.RequestBody) was never meant to be called directly. It was added as a workaround to pass though data from AddBody() but then other things became dependent on it so it stuck around. It was a terrible decision in hindsight but it's too late to change it in the 1xx version line. If I ever build another version I'll make this more obvious.

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • 2
    If it's not really meant to be used, you could mark it as Obsolete, so then existing programs can keep working, but it doesn't show up in Intellisense anymore, this would prevent most users from calling it in their own code? – Stephanvs Feb 24 '12 at 19:29
  • I don't think its quite obsolete. Adding attribute `[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]` could be a happy medium (?). – Andrew Young Feb 24 '12 at 20:54
  • I don't want to change anything about it because it's a well-publicized workaround to another issue. – John Sheehan Feb 24 '12 at 22:06
  • Thanks - I thought i couldn't use the contentType in `AddParameter`, because I just got weird results from that. But it seems, the Problem lies somewhere else. (RestSharp can connect to HTTPS-Resources, am I right? :) – DIF Feb 27 '12 at 10:56
  • It can. Though if you're using mono there are some considerations to be made. – John Sheehan Feb 29 '12 at 20:51
1

It is possible changing Content-Type when you set the body content. The NAME parameter for Body sets the Content-Type.

oRequest.Parameters.Add(new Parameter() { Name = "application/json;charset=UTF-8", Type = ParameterType.RequestBody, Value = sBody });
Allan Zeidler
  • 317
  • 2
  • 7