0% found this document useful (0 votes)
135 views

WCF Rest XML Post

The document describes an issue where a client is getting a "Bad Request" error when calling a POST method on a WCF REST service. The service is configured to accept XML payloads but the client's XML payload is missing the namespace, which is required. Adding the namespace to the root element of the XML resolves the error.

Uploaded by

Rajesh Samanth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

WCF Rest XML Post

The document describes an issue where a client is getting a "Bad Request" error when calling a POST method on a WCF REST service. The service is configured to accept XML payloads but the client's XML payload is missing the namespace, which is required. Adding the namespace to the root element of the XML resolves the error.

Uploaded by

Rajesh Samanth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

WCF REST POST XML - The remote server returned an

error: (400) Bad Request


I have exposed my REST API service using WCF Service Library and started by console
application. when I try to consume the service from client ( another console application), I am
getting "Bad Request" exception.
please see the code below :[ServiceContract(Namespace = "http://www.test.com/Enrollment")]
public interface IEnrollService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "enroll",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle= WebMessageBodyStyle.Bare)]
string EnrollData(Enroll enrData);
}
public class EnrollService : IEnrollService
{
public string EnrollData(Enroll enrData)
{
return "Hello, your test data is " + enrData.AccountNumber;
}
}

DataContract
[DataContract(Namespace = "http://www.test.com/Enrollment")]
public class Enroll
{
[DataMember]
public string ClientId
{
get;
set;
}
[DataMember]
public string AccountNumber
{
get;
set;
}
[DataMember]
public string TaxId
{
get;
set;
}
[DataMember]
public string TaxType
{
get;

set;
}
[DataMember]
public string EmailAddress
{
get;
set;
}
[DataMember]
public string Pin
{
get;
set;
}
[DataMember]
public string State
{
get;
set;
}
}

app.config settings
<system.serviceModel>
<services>
<service name="OEA.REST.EnrollService" behaviorConfiguration="OEABehavior">
<endpoint address="http://localhost:8010/OEA/Service/EnrollService"
binding="webHttpBinding" behaviorConfiguration="webHttp"
contract="OEA.REST.IEnrollService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8010/OEA/Service/EnrollService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="OEABehavior">
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

client code:
HttpWebRequest req = null;
HttpWebResponse res = null;

try
{
string url = "http://localhost:8010/OEA/Service/EnrollService/enroll";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/xml; charset=utf-8";
req.Timeout = 30000;
req.Headers.Add("SOAPAction", url);
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.Load(@"d:\test.xml");
string sXML = xmlDoc.InnerXml;
req.ContentLength = sXML.Length;
System.IO.StreamWriter sw = new
System.IO.StreamWriter(req.GetRequestStream());
sw.Write(sXML);
sw.Close();
res = (HttpWebResponse)req.GetResponse();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}

content of test.xml
<Enroll>
<ClientId>000</ClientId>
<AccountNumber>123</AccountNumber>
<TaxId>123</TaxId>
<TaxType>123</TaxType>
<EmailAddress>123</EmailAddress>
<Pin>123</Pin>
<State>122</State>
</Enroll>
I am getting "Bad Request" exception on req.GetResponse() method call. Just started learning
REST. I am stuck up with POST request. Any help would be highly appreciated. Thanks in
advance.

Answers
Your POST payload should be :
<Enroll xmlns="http://www.test.com/Enrollment">
<ClientId>000</ClientId>
<AccountNumber>123</AccountNumber>
<TaxId>123</TaxId>
<TaxType>123</TaxType>
<EmailAddress>123</EmailAddress>
<Pin>123</Pin>
<State>122</State>
</Enroll>
I updated the xml as per your response. That issue got resolved. Thanks a lot

You might also like