Reputation: 5596
I'm trying to consume a rest service(wcf) using restsharp
This is my service
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "/PEmploy", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
Employee PostGetEmploy(Employee emp);
}
[DataContract]
public class Employee
{
[DataMember]
public int EmpNo { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public string DeptName { get; set; }
}
and this is how I call it
var client = new RestClient("http://localhost:14437/Service.svc");
var request = new RestRequest("XmlService/PEmploy", Method.POST);
myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" };
request.AddParameter("Employee", emp);
RestResponse<myRef.Employee> response = (RestResponse<myRef.Employee>)client.Execute<myRef.Employee>(request);
and this is the exception that I get
Exception:Caught: "Data at the root level is invalid. Line 1, position 1." (System.Xml.XmlException)
A System.Xml.XmlException was caught: "Data at the root level is invalid. Line 1, position 1."
I've tried serialization but still got the same exception. What am I doing wrong?
Upvotes: 4
Views: 4255
Reputation: 5596
I added
request.RequestFormat = DataFormat.Json
and this did the trick.
Full Code
var client = new RestClient("http://localhost:14437/Service.svc");
var request = new RestRequest("XmlService/PEmploy", Method.POST);
request.RequestFormat = DataFormat.Json;
myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" };
request.AddParameter("Employee", emp);
RestResponse<myRef.Employee> response = (RestResponse<myRef.Employee>)client.Execute<myRef.Employee>(request);
Upvotes: 5