Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with object as parameter in REST API Service
    text
    copied!<p>EDIT: Apparently what I have is working. I had been testing using the xml generated from the serialized object, which wasn't working. I was messing with it more just now and decided to try using the dynamic XML that I am creating (same as posted here), and it worked. I think maybe it was because when i was trying it that way before I wasn't using the same namespaces... However, how come using the xml from the serialized object doesn't work? I noticed it uses different namespace in the xml when it is created from the object. Why is that?</p> <p><strong>Original post:</strong> I am having an issue with passing a custom object to a REST API I'm creating. Right now I am just trying to test it by posting an object of type Lead to a WebInvoke method. My service is written with WCF. The Lead object is in a separate class library project that will by my business logic layer.</p> <p>I'm posting using a regular ASP .net page. You'll see in that section of code below that I tried building the xml in two ways, manually, and also by Serializing the object as xml and using that as the POST data. Neither work, and I'm getting this error: The remote server returned an error: (400) Bad Request.</p> <p>I know the service is configured correctly, as I'm able to call and get a response from the other WebInvoke method I have written, which is a GET method.</p> <p>Here is the releveant code: Service interface code:</p> <pre><code>[ServiceContract(Namespace = "http://www.mytestnamespace.com/test")] public interface IRESTService { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "leads", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] string AddLead(Lead lead); [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "lead/{id}", ResponseFormat = WebMessageFormat.Xml)] string LeadData(string id); } </code></pre> <p>Service code:</p> <pre><code>public class RESTService : IRESTService { public string AddLead(Lead lead) { return "AddLead Hit"; } public string LeadData(string id) { return "LeadData was hit, id=" + id; } } </code></pre> <p>Lead object Code:</p> <pre><code>[DataContract(Name="Lead", Namespace = "http://www.mytestnamespace.com/test")] public class Lead { [DataMember(Name="FirstName")] public string FirstName { get; set; } [DataMember(Name = "LastName")] public string LastName { get; set; } [DataMember(Name = "Email")] public string Email { get; set; } [DataMember(Name = "Phone")] public string Phone { get; set; } } </code></pre> <p>And finally the code i'm using on the aspx page to post the data:</p> <pre><code>try { Lead l = new Lead(); l.FirstName = "John"; l.LastName = "Doe"; l.Email = "John.Doe@gmail.com"; l.Phone = "5555551234"; XmlSerializer ser = new XmlSerializer(typeof(Lead)); StringWriter sw = new StringWriter(); ser.Serialize(sw, l); string s = sw.ToString(); /*XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); XmlElement root = doc.CreateElement("Lead"); root.SetAttribute("xmlns", "http://www.mytestnamespace.com/test"); //root.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); doc.InsertBefore(dec, doc.DocumentElement); doc.AppendChild(root); XmlElement firstName = doc.CreateElement("FirstName"); XmlElement lastName = doc.CreateElement("LastName"); XmlElement email = doc.CreateElement("Email"); XmlElement phone = doc.CreateElement("Phone"); firstName.AppendChild(doc.CreateTextNode("John")); lastName.AppendChild(doc.CreateTextNode("Doe")); email.AppendChild(doc.CreateTextNode("John.Doe@gmail.com")); phone.AppendChild(doc.CreateTextNode("8885551234")); root.AppendChild(firstName); root.AppendChild(lastName); root.AppendChild(email); root.AppendChild(phone);*/ HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:54966/RESTService.svc/leads")); req.Method = "POST"; req.ContentType = "application/xml"; //byte[] formData = UTF8Encoding.UTF8.GetBytes(doc.InnerXml); byte[] formData = UTF8Encoding.UTF8.GetBytes(s); req.ContentLength = formData.Length; using (Stream post = req.GetRequestStream()) { post.Write(formData, 0, formData.Length); } string result = null; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { StreamReader reader = new StreamReader(resp.GetResponseStream()); result = reader.ReadToEnd(); } } catch (Exception exc) { throw exc; } </code></pre>
 

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