400 bad request to send xml payload to WCF REST service

I know that there are several posts asking about 400 error, and I believe that I have read all of them, but I think that the problem I am facing is different.

This is my WCF service contract.

[WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] Stream GetData(string key, string id, string data); 

And this is the code that I use to send a request for my vacation svc

 request.RequestUri = new Uri("http://localhost:3138/v1/cust_key/company1/prod_id/testProductID"); request.ContentType = "application/xml"; request.HttpMethod = "POST"; string xml = @"<Product><name>dell 400</name><price>400 dollars</price></Product>"; byte[] message = Encoding.ASCII.GetBytes(xml); string data = Convert.ToBase64String(message); response = request.MakeWebRequest(null, data); 

This gave me 400 errors with an error. I tried changing the xml line to the next two and they also produce 400 errors

 <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> <![CDATA[<Product><name>dell 400</name><price>400 dollars</price></Product>]]> </string> 

or

 <![CDATA[<Product><name>dell 400</name><price>400 dollars</price></Product>]]> 

If the xml payload is an empty string, then everything is fine and 200 is returned. Can someone give me a hand?

Edit: section of my web.config. he goes out of the box from the WCF REST Service Template 40 (CS)

 <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <standardEndpoints> <webHttpEndpoint> <!-- Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the <standardEndpoint> element below --> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> </webHttpEndpoint> </standardEndpoints> 

+4
c # rest xml wcf
source share
1 answer

The second example using the <string> element should work. If you know the XML schema that you get, you can do the following:

 [WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] //Almost exacely the same except String is now Product in the method Parameters Stream GetData(string key, string id, Product data); [DataContract(Namespace = "")] public class Prodect { [DataMember] public string name { get; set; } [DataMember] public string price { get; set; } } 

Then use the client code from your message and it should work fine. On the other hand, if you want your web service to dynamically accept different XML that was not clearly defined by data contracts, you can use XElement as follows:

 [WebInvoke(UriTemplate = "/cust_key/{key}/prod_id/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] //Almost exacely the same except String is now XElement Stream GetData(string key, string id, XElement data); 
+3
source share

All Articles