title: WCF Rest service - passing XML with POST
body: <p>I've inherited an ASPX page which is POSTed to by a client. I can't change the client. I want to redo this as a WCF Rest service. The ASPX page retrieves XML data from the request in the Page_load event</p>
  
  <p>After a good bit of reading I have a WCF Rest service setup which works with the same test data, but only if my test code sets the contentType to:</p>
  
  <pre><code>  request.ContentType = @"application/x-www-form-urlencode";
  </code></pre>
  
  <p>The client calling me will just be using:</p>
  
  <pre><code>  request.ContentType = @"text/xml";
  </code></pre>
  
  <p>and this fails with a <strong>400 error</strong>.</p>
  
  <p>Here's my interface:</p>
  
  <pre><code>public interface ISmsReceive {
      [OperationContract]
      [WebInvoke(Method = "POST",
      ResponseFormat = WebMessageFormat.Xml,
      RequestFormat = WebMessageFormat.Xml,
      BodyStyle = WebMessageBodyStyle.Bare,
      UriTemplate = "xml")]
      string Receive(Stream ID);
  }
  </code></pre>
  
  <p>and here's a sample call which works:</p>
  
  <pre><code>  string s = "http://localhost:1773/SmsReceive.svc/xml";
    String xmlString = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;" +
              "&lt;accountrequest&gt;" + // xmlns=\"http://tempuri.org/\"&gt;" +
              "&lt;accountprocessinboundmessage&gt;" +
              "&lt;inboundnumber&gt;&lt;![CDATA[123]]&gt;&lt;/inboundnumber&gt;" +
              "&lt;dateinsert&gt;&lt;![CDATA[1/1/2003 12:00:00 PM]]&gt;&lt;/dateinsert&gt;" +
              "&lt;inboundcli&gt;&lt;![CDATA[+123]]&gt;&lt;/inboundcli&gt;" +
              "&lt;message&gt;&lt;![CDATA[blah blah]]&gt;&lt;/message&gt;" +
              "&lt;/accountprocessinboundmessage&gt;" +
              "&lt;/accountrequest&gt;";
  
    WebRequest request = WebRequest.Create(s);
  
    request.Method = "POST";
    // Works with this
    request.ContentType = @"application/x-www-form-urlencode";
  
    // Fails with this
    //request.ContentType = @"text/xml";
  
    xmlStream = (request.GetRequestStream());
    StreamWriter writer = new StreamWriter(xmlStream);
    writer.Write(xmlString);
    writer.Flush();
    writer.Close();
  
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    String responseString = reader.ReadToEnd();
  </code></pre>
  
  <p>I read in one post that you must define an xmlns in the XML - but even with code above uncommented which inserts this it still fails.</p>
  
  <p>Cheers for any pointers,</p>
  
  <p>Ray</p>