POST Request for REST webservicehttp://forums.asp.net/t/1789384.aspx/1?POST+Request+for+REST+webserviceMon, 09 Apr 2012 03:37:28 -040017893844917078http://forums.asp.net/p/1789384/4917078.aspx/1?POST+Request+for+REST+webservicePOST Request for REST webservice <p>Hi all,</p> <p>I've written code for exposing methods as REST Webservice. My code is working for GET Requests but for POST requests, it is giving 400 Bad Request error.</p> <p>I have an object which have DateTime type variables and these variables can be null.</p> <p>For Post request, when I leaves these variable values as blank, then I get 400 Bad request error; otherwise it works.</p> <p>So how can I achieve the nullable DateTime variables.</p> <p>Kindly suggest.</p> <p></p> <p>Thanks in advance.</p> 2012-04-05T04:35:52-04:004918227http://forums.asp.net/p/1789384/4918227.aspx/1?Re+POST+Request+for+REST+webserviceRe: POST Request for REST webservice <p>Nullable&lt;DateTime&gt; or DateTime?</p> 2012-04-05T14:04:32-04:004919119http://forums.asp.net/p/1789384/4919119.aspx/1?Re+POST+Request+for+REST+webserviceRe: POST Request for REST webservice <p>Hi agarwal.peeush,</p> <p>Are you using WCF to build the REST service? If so, WCF 4.0 REST service provides a &quot;help page&quot; feature which can generate the sample syntax of HTTP GET/POST request used for invoking the REST service operations</p> <p>#WCF Web HTTP Service Help Page <br> <a href="http://msdn.microsoft.com/en-us/library/ee230442.aspx">http://msdn.microsoft.com/en-us/library/ee230442.aspx</a></p> <p>So for a REST operation(use HTTP POST) like:</p> <pre class="prettyprint">[WebInvoke] [OperationContract] DateTime? GetDateTime(DateTime? date);</pre> <p>&nbsp;</p> <p>The help page will show you the HTTP POST request format like:</p> <p>&nbsp;</p> <p><em><strong>The following is an example request Xml body:</strong></em></p> <pre class="prettyprint">&lt;dateTime xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;1999-05-31T11:20:00&lt;/dateTime&gt;</pre> <p>&nbsp;</p> <p><em><strong>The following is an example request Json body:</strong></em></p> <pre class="prettyprint">"\/Date(928120800000+0800)\/"</pre> <p><br> here are some further reference on WCF REST programming:</p> <p>#WCF Web HTTP Programming Model Overview <br> <a href="http://msdn.microsoft.com/en-us/library/bb412172.aspx">http://msdn.microsoft.com/en-us/library/bb412172.aspx</a></p> <p>#WCF REST Service Template 40(CS) <br> <a href="http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd">http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd</a></p> 2012-04-06T07:33:50-04:004922126http://forums.asp.net/p/1789384/4922126.aspx/1?Re+POST+Request+for+REST+webserviceRe: POST Request for REST webservice <p>Thanks for the input, but I guess I need to be more clear with my requirement. For this I am attaching my code with the POST request that I want user to send.</p> <p>Code for handling POST request:</p> <pre class="prettyprint"><em>public String PostCustomer(CreateCustomer cust) { //if (!WebService.ValidateCreateCustomer(newCustomer)) //{ // return WebService.CreateCustomer(newCustomer); // //return DbClass.PersistCustomer(Connection.Connect(), cust); //} return &quot;I've got this &quot; &#43; cust; //Just for check that my request is being handled or not }</em></pre> <pre class="prettyprint"><em>public class CreateCustomer { public enum NewDeviceBehaviorType { MANAGED, UNMANAGED, UNASSIGNED }; public enum MailCultureType { }; public string Name { get; set; } public DateTime BillingStartDate { get; set; } public DateTime BillingEndDate { get; set; } public int levelID { get; set; } public String Comment { get; set; } private string newdeviceBehavior; public string NewDeviceBehavior { get { return newdeviceBehavior; } set { this.newdeviceBehavior = value; } } public List&lt;string&gt; MailTo { get; set; } public List&lt;string&gt; MailCC { get; set; } public List&lt;string&gt; MailBCC { get; set; } public String MailCulture { get; set; } }</em></pre> <pre class="prettyprint"><em><br /><br /></em></pre> <p>And using Fiddler, I'm sending the POST request as following:</p> <p>POST request Body:-</p> <p><em>&lt;CreateCustomer xmlns=&quot;http://schemas.datacontract.org/2004/07/DCAService.Models&quot;&gt;</em><br> <em>&nbsp; &lt;BillingEndDate&gt;&lt;/BillingEndDate&gt;</em><br> <em>&nbsp; &lt;BillingStartDate&gt;&lt;/BillingStartDate&gt;</em><br> <em>&nbsp; &lt;Comment&gt;Testing&lt;/Comment&gt;</em><br> <em>&nbsp; &lt;MailBCC&gt;</em><br> <em>&nbsp;&nbsp;&nbsp; &lt;string xmlns=&quot;http://schemas.microsoft.com/2003/10/Serialization/Arrays&quot;&gt;&lt;/string&gt;</em><br> <em>&nbsp; &lt;/MailBCC&gt;</em><br> <em>&nbsp; &lt;MailCC&gt;</em><br> <em>&nbsp;&nbsp;&nbsp; &lt;string xmlns=&quot;http://schemas.microsoft.com/2003/10/Serialization/Arrays&quot;&gt;&lt;/string&gt;</em><br> <em>&nbsp; &lt;/MailCC&gt;</em><br> <em>&nbsp; &lt;MailCulture&gt;en-US&lt;/MailCulture&gt;</em><br> <em>&nbsp; &lt;MailTo&gt;</em><br> <em>&nbsp;&nbsp;&nbsp; &lt;string xmlns=&quot;http://schemas.microsoft.com/2003/10/Serialization/Arrays&quot;&gt;somemail@some.com&lt;/string&gt;</em><br> <em>&nbsp; &lt;/MailTo&gt;</em><br> <em>&nbsp; &lt;Name&gt;Someone&lt;/Name&gt;</em><br> <em>&nbsp; &lt;NewDeviceBehavior&gt;UNMANAGED&lt;/NewDeviceBehavior&gt;</em><br> <em>&nbsp; &lt;levelID&gt;22&lt;/levelID&gt;</em><br> <em>&lt;/CreateCustomer&gt;</em></p> <p>POST request Header:-</p> <p><em>User-Agent: Fiddler</em><br> <em>Host: localhost:8000</em><br> <em>Content-Length: 707</em><br> <em>Content-Type: text/xml</em></p> <p>And for this request I'm getting <strong>HTTP/1.1 400 Bad Request</strong>.</p> <p></p> <p>Kindly suggest.<br> <br class="prettyprint"> </p> 2012-04-09T03:37:28-04:00