I have a REST POST method as follows:
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
string GetFromXml(XElement xmlString);
I am trying to do a post operation from my client using the following code:
var client = new RestClient();
client.BaseUrl = "http://localhost/XMLRestService/XmlService.svc";
var request = new RestRequest(Method.POST);
request.Resource = "GetFromXml";
client.AddDefaultHeader("Content-Type", "text/xml");
request.AddBody(obj, "XMLRestService");
var response = client.Execute(request);
When i do the above i get a 400 Bad request. I then enabled tracing on the WCF Service. And the stack trace gave me a Unrecognized Message version excpetion that is thrown my the System.ServiceModel.CommunicationException
class.
I am unable to post the request successfully. Help appreciated.
I finally found the root cause to the exception. The below code works perfect:
var client = new RestClient();
client.BaseUrl = serviceBaseUrl;
var request = new RestRequest(method){RequestFormat = DataFormat.Xml};
request.Resource = resourceUrl;
request.AddParameter("text/xml",requestBody, ParameterType.RequestBody);
var response = client.Execute(request);
The requestBody being sent as parameter should be a serialized xmlString.
NOTE: If the composite type exposed on the REST Service is using DataContractSerializer then make sure to generate the requestBody using DataContractSerializer and if uses XmlSerializer then generate the requestBody using XmlSerializer.