Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does RestSharp overwrite manually set Content-Type?

Tags:

c#

restsharp

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)?

like image 247
DIF Avatar asked Feb 24 '12 14:02

DIF


People also ask

Does RestSharp reuse HTTP client?

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use. That's why it is safe (and recommended) to create a new instance of RestClient per request.

Is RestSharp asynchronous?

Although RestSharp can call any API using the HTTP protocol, the purpose of RestSharp is to consume the REST APIs. RestSharp supports both synchronous and asynchronous requests.

What is RestSharp default timeout?

RestSharp is using HttpWebRequest under the hood, which has a default timeout of 100 seconds.

What is RestSharp used for?

RestSharp is an open source HTTP client library that makes it easy to consume RESTful services. RestSharp provides a developer friendly interface to work with RESTful services while abstracting the internal intricacies of working with HTTP requests. RestSharp supports both synchronous and asynchronous requests.


2 Answers

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.

like image 194
John Sheehan Avatar answered Sep 19 '22 19:09

John Sheehan


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 });
like image 45
Allan Zeidler Avatar answered Sep 21 '22 19:09

Allan Zeidler