title: HTTP Post to test WCF REST End Point. Using an XML formatted contract
body: <p>I have a webservice with a contract that is required to use XML. 
  (It has other JSON decorated contracts and they are consumed fine by the users)</p>
  
  <p>I want  to test the XML contract via the REST endpoint. (It works fine from a unit test using the default SOAP context)
  My efforts of using a roll your own Http Post in a unit test  give Error 400 </p>
  
  <p>After about 3 days and the test code looking like a bomb site I am still stuck.</p>
  
  <p>How hard can it be???</p>
  
  <p>VS2013 FrameWork 4.5.1.</p>
  
  <p><strong>All I am trying to do is prove with the unit test (or Fiddler or Postman) that the  REST endpoint will handle a Post with an xml payload.</strong></p>
  
  <p>If someone could put up some sample  HTTP POST c# code that they expect to consume the simple test contract below I would be grateful. I have tried more variations of what is available on the Net than I can remember and am now totally confused.</p>
  
  <p><strong>TEST CONTRACT:</strong></p>
  
  <pre><code>[WebInvoke(
          Method = "POST",
          UriTemplate = "Testing",
          BodyStyle = WebMessageBodyStyle.WrappedRequest,
          ResponseFormat = WebMessageFormat.Xml,
          RequestFormat = WebMessageFormat.Xml)]
      [OperationContract]
      void Testing(TestData test);
  </code></pre>
  
  <p>This is implemented by:</p>
  
  <pre><code> public void Testing(TestData test)
      {
          int x = test.MyInt;
          string y = test.MyString;
      }
  
  
   [DataContract]
  public class TestData
  {
      [DataMember]
      public int MyInt { get; set; }
      [DataMember]
      public string MyString { get; set; }
  }
  </code></pre>
  
  <p>The unit test code is:</p>
  
  <pre><code>                string url = "http://myService.svc/REST/Testing";
              DellbertCouponParking.TestData t = new DellbertCouponParking.TestData();
              t.MyInt = 2;
              t.MyString = "HelloWorld";
  
              XmlSerializer xsSubmit = new XmlSerializer(typeof(DellbertCouponParking.TestData));
              StringWriter sw = new StringWriter();
              XmlWriter writer = XmlWriter.Create(sw);
              xsSubmit.Serialize(writer, t);
              string xml = sw.ToString();
  
              byte[] formData = System.Text.Encoding.ASCII.GetBytes(xml);
              HttpWebRequest req = WebRequest.Create(new Uri(url))
                                       as HttpWebRequest;
              req.Method = "POST";
              req.ContentLength = formData.Length;
              req.ContentType = "text/xml; encoding='utf-8'";
              // Send the request:
              using (Stream requestStream = req.GetRequestStream())
              {
                  requestStream.Write(formData, 0, formData.Length);
                  requestStream.Close();
              }
  
              // Pick up the response:
              string result = null;
              using (HttpWebResponse resp = req.GetResponse()
                                            as HttpWebResponse)
              {
                  StreamReader reader =
                      // ReSharper disable once PossibleNullReferenceException
                      new StreamReader(resp.GetResponseStream());
                  result = reader.ReadToEnd();
              }
              Assert.IsTrue(result.Length &gt; 0);
  </code></pre>
  
  <p>There are  'JSON' contracts that the REST Endpoint is servicing OK.
  This is my first attempt at an xml contract.</p>
  
  <p>Since my original posting of this question, I have proven that the testing code is OK by posting to httpbin.org/post</p>
  
  <p>So I guess my problem comes down to:</p>
  
  <ul>
  <li>Possible bad decoration of the contract </li>
  <li>Web Site configuration. (Internal test server) </li>
  </ul>
  
  <p>Further information:</p>
  
  <p>The web.config holds:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;client&gt;
    &lt;endpoint address="https://sec.paymentexpress.com/PxF/pxf.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPxFusion"
      contract="DPSFusion.IPxFusion" name="BasicHttpBinding_IPxFusion" /&gt;
  &lt;/client&gt;
  &lt;bindings&gt;
    &lt;basicHttpBinding&gt;
      &lt;binding name="basicHttp" maxBufferPoolSize="20000000" maxBufferSize="20000000"
        maxReceivedMessageSize="20000000"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="200000000"
          maxArrayLength="200000000" /&gt;
      &lt;/binding&gt;
      &lt;binding name="BasicHttpBinding_IPxFusion" maxBufferPoolSize="20000000"
        maxBufferSize="20000000" maxReceivedMessageSize="20000000"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="200000000"
          maxArrayLength="200000000" /&gt;
        &lt;security mode="Transport" /&gt;
      &lt;/binding&gt;
    &lt;/basicHttpBinding&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="JsonBinding" maxBufferSize="20000000" maxBufferPoolSize="20000000"
        maxReceivedMessageSize="20000000"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="200000000"
          maxArrayLength="200000000" /&gt;
      &lt;/binding&gt;       
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;services&gt;
    &lt;service name="CouponParkingWCF.CouponService"&gt;
      &lt;endpoint name="SoapEndPoint" address="SOAP" binding="basicHttpBinding" contract="CouponParkingWCF.ICouponService" /&gt;
      &lt;endpoint name="JSON" address="REST" behaviorConfiguration="JsonBehavior" binding="webHttpBinding" contract="CouponParkingWCF.ICouponService" /&gt;       
    &lt;/service&gt;
  &lt;/services&gt;
  &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="JsonBehavior"&gt;
        &lt;webHttp /&gt;
      &lt;/behavior&gt;        
    &lt;/endpointBehaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name=""&gt;
        &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;!-- &lt;protocolMapping&gt;
    &lt;add binding="basicHttpsBinding" scheme="https"/&gt;
  &lt;/protocolMapping&gt;--&gt;
  &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>The IIS Log shows:
  2015-07-20 23:13:25 192.168.0.235 POST /wsCouponParking/CouponParking.svc/REST/Testing - 80 - 192.168.0.38 - - 400 0 0 203</p>
  
  <p>Would appreciate any guidance on what I am missing.
  Thanks
  bob</p>