iSahilSharma iSahilSharma - 8 months ago 60
C# Question

Passing a class object as input parameter to a RESTful WCF service with POST request

I have created a RESTful WCF service and trying to pass a class as an parameter to a POST request using Fiddler but facing errors like:
"HTTP/1.1 400 Bad Request"

The Interface - IXmlService.cs


public interface IXmlService
[OperationContract(Name = "Read")]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Read", BodyStyle = WebMessageBodyStyle.Wrapped)]
bool ReadData(Order data);

[OperationContract(Name = "Generate")]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Generate/")]
bool GenerateXml();

The Implementation - XmlService.cs


public class XmlService : IXmlService
public bool ReadData(Order data)
bool result = false;
var path = "@C:\\order.xml";
XmlSerializer serializer;
TextWriter writer;

if (data != null)
//serializer = new XmlSerializer(typeof(Order), new XmlRootAttribute("HEADER"));
serializer = new XmlSerializer(typeof(Order)); //No need to provide XmlRootAttribute as I have defined it on Order Model.
writer = new StreamWriter(path);
serializer.Serialize(writer, data);
result = true;
catch (Exception)
return result;

public bool GenerateXml()
throw new NotImplementedException();


The Data Model - Order.cs


[XmlRootAttribute("OrderDetails", Namespace = "", IsNullable = false)]
public class Order
// The XmlArrayAttribute changes the XML element name
// from the default of "OrderedItems" to "Items".
public int OrderId { get; set; }

public string Owner { get; set; }

// Setting the IsNullable property to false instructs the
// XmlSerializer that the XML attribute will not appear if
// the City field is set to a null reference.
[XmlElementAttribute(IsNullable = false)]
public string Info { get; set; }

public string Recipient { get; set; }

//public DateTime CreatedOn { get; set; }




<service name="WCF_XML_Service.XmlService" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<add baseAddress="http://localhost:16999"/>

<endpoint address="/xml/" binding="webHttpBinding" contract="WCF_XML_Service.IXmlService" behaviorConfiguration="Web"/>

<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>

<behavior name="Web">
<webHttp helpEnabled="true"/>


I tried different approaches by converting binding to 'webHttpBinding' in Web.Config. Also, I tried adding ' BodyStyle = WebMessageBodyStyle.Wrapped' into the WebInvoke attribute but still not able to hit the service using fiddler.

Fiddler Request:

Url - http://localhost:16999/XmlService.svc/xml/Read
Method - POST
Request Header:
User-Agent: Fiddler
Host: localhost:16999
Content-Type: text/xml
Content-Length: 155

Request Body:
"OrderId": "1",
"Owner": "Sam Shipping",
"Info": "First delivery shipment",
"Recipient": "Singapore Shipping Corporation"

Answer Source

Well, after wasting three days I finally figured out the error which is "Content Type" : I was using "Content-Type: text/xml" for JSON data which has to be "Content-Type: text/json".

Guess i need to clean my spectacles on daily basis for now on :)