Note that there are some explanatory texts on larger screens.

plurals
  1. POAnother (400) bad request with C# REST Client: How to POST pure XML?
    text
    copied!<p>I'm having a hard time to get a simple RESTful web service application working with my C# REST client. I'm trying to send pure XML using the POST method. </p> <p>I have created a WCF Rest Service which is up and running (I can see the Service test page in my browser). Also I can call my WCF Rest Service via Fiddler and it responds correctly (I'll get a return value).</p> <p>But the big problem is the REST client. I've done everything using all kinds of guides. In addition, the same client code works with the Java-based REST service. But in .NET all I get is the error "The remote server returned an error: (400) Bad Request." This exception happens after Request.GetResponse()</p> <p>As I have developed WCF services I've used to specify configuration settings for both client (app.config) and server (web.config) but how is the situation with .NET-based RESTful web services? I mean, do I need to specify identical binding settings (webHttpBinding) for both, client (app.config) and server (web.config)?</p> <p>Here is the client-side code:</p> <pre><code>XmlDocument TestDocument = new XmlDocument(); TestDocument.Load(XMLFilePath); byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(TestDocument.OuterXml); Uri uri = new Uri("http://localhost:2319/MyRESTService.svc/receive"); HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(uri); Request.ContentLength = RequestBytes.Length; Request.Method = "POST"; Request.ContentType = "text/xml"; Stream RequestStream = Request.GetRequestStream(); RequestStream.Write(RequestBytes, 0, RequestBytes.Length); RequestStream.Close(); HttpWebResponse response = (HttpWebResponse)Request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string ResponseMessage = reader.ReadToEnd(); response.Close(); </code></pre> <p>The web.config for my WCF REST Service looks like this (should I specify the same settings in the app.config in the C# REST client?): </p> <pre><code>&lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="MyRESTService.MyRESTService"&gt; &lt;endpoint binding="webHttpBinding" contract="MyRESTService.IMyRESTService" behaviorConfiguration="webHttp"/&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="webHttp"&gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; </code></pre> <p></p> <p>The interface for my WCF REST Service looks like this:</p> <pre><code>[ServiceContract] public interface IMyRESTService [OperationContract] [WebInvoke( Method = "POST", UriTemplate = "/receive", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle=WebMessageBodyStyle.Bare)] string Receive(string xml); </code></pre> <p>As I understand this is a client-side problem and the exception is raised due to problems found on the client-side. But what is specifically causing this exception in my C# Rest Client? </p> <p>The XML looks like this:</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt; &lt;MATMAS02&gt; &lt;IDOC BEGIN="1"&gt; &lt;EDI_DC40 SEGMENT="1"&gt; ...... </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