Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF REST POST XML - The remote server returned an error: (400) Bad Request
    primarykey
    data
    text
    <p>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.</p> <p>please see the code below :-</p> <pre><code>[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; } } </code></pre> <h2>DataContract</h2> <pre><code>[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; } } </code></pre> <h2>app.config settings</h2> <pre><code>&lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="OEA.REST.EnrollService" behaviorConfiguration="OEABehavior"&gt; &lt;endpoint address="http://localhost:8010/OEA/Service/EnrollService" binding="webHttpBinding" behaviorConfiguration="webHttp" contract="OEA.REST.IEnrollService" /&gt; &lt;host&gt; &lt;baseAddresses&gt; &lt;add baseAddress="http://localhost:8010/OEA/Service/EnrollService" /&gt; &lt;/baseAddresses&gt; &lt;/host&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="webHttp"&gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="OEABehavior"&gt; &lt;serviceMetadata httpGetEnabled="True" /&gt; &lt;serviceDebug includeExceptionDetailInFaults="True" /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt; &lt;/system.serviceModel&gt; </code></pre> <h2>client code:</h2> <pre><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); } </code></pre> <h2>content of test.xml</h2> <pre><code>&lt;Enroll&gt; &lt;ClientId&gt;000&lt;/ClientId&gt; &lt;AccountNumber&gt;123&lt;/AccountNumber&gt; &lt;TaxId&gt;123&lt;/TaxId&gt; &lt;TaxType&gt;123&lt;/TaxType&gt; &lt;EmailAddress&gt;123&lt;/EmailAddress&gt; &lt;Pin&gt;123&lt;/Pin&gt; &lt;State&gt;122&lt;/State&gt; &lt;/Enroll&gt; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload