title: Getting a POST endpoint to work in self-hosted (WebServiceHost) C# webservice?
body: <p>So, I have been messing around with webservices for a while now, and I keep getting back to some basics, that I never seem to get right.</p>
  
  <h1>Question 1:</h1>
  
  <p>When using a WebServiceHost in .NET/C#, you can define a method/endpoint as using GET/POST/etc. Setting up a GET-method is easy and it works pretty much directly, and its easy to understand how it works. For example:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "GET", UriTemplate = "/PutMessage/{jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
  string PutMessage(string jsonString);
  </code></pre>
  
  <p>If I call http:///MyWebService/PutMessage/{MyJsonString} I get passed on the the method, and all is well (more or less).</p>
  
  <p>But then what does it mean when I define this as a <strong>POST</strong> instead?</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", UriTemplate = "/PutMessage/{jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
  string PutMessage(string jsonString);
  </code></pre>
  
  <p><strong>What does the UriTemplate do here?</strong> If I do a POST, I expect the data to be contained not in the URI, but in the "data section" of the post. But do I define the variable name in the data section? How does the WebServiceHost/.NET know that what is contained in the "data section" of the post is to be put into the variable jsonString? How do I post the data from the client side (not C#, let's say JQuery instead) so that it is interpreted correctly on the serer side?</p>
  
  <p>(And how does the WebMessageFormat affet things? I have read everywhere about this (MSDN, Stackoverflow etc) but haven't found a clear and good answer.)</p>
  
  <h1>Question 2:</h1>
  
  <p>In my attempts to understand this, I thought I'd make a very simple POST-method, like this:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke]
  string PutJSONRequest(string pc);
  </code></pre>
  
  <p>I then <em>try</em> call this method using Fiddler, but that does not work at all. I just get a 400 error back, saying "HTTP/1.1 400 Bad Request". I have a breakpoint on the very first line in the code of the method, and the method itself contains nothing:</p>
  
  <pre><code>public string PutJSONRequest(string pc)
  {
      return null;
  }
  </code></pre>
  
  <p><strong>Again, how does .NET know that what I POSTed using Fiddler should be contained in the "string pc"?</strong> How does it interpret it as a string, and what type of string (UT8, ASCII etc)?</p>
  
  <p>This is the RAW HTTP request, sent from Fiddler:</p>
  
  <pre><code>POST http://&lt;myip&gt;:8093/AlfaCustomerApp/PutJSONRequest HTTP/1.1
  User-Agent: Fiddler
  Host: &lt;myip&gt;:8093
  Content-Length: 3
  Content-type: application/x-www-form-urlencoded; charset=UTF-8
  
  asd
  </code></pre>
  
  <p>and it doesnt matter what type of Content-type I use, as far as I can see.</p>
  
  <p>The response is a standard-thing, that I am not in control of myself:</p>
  
  <pre><code>HTTP/1.1 400 Bad Request
  Content-Length: 1165
  Content-Type: text/html
  Server: Microsoft-HTTPAPI/2.0
  Date: Mon, 15 Oct 2012 15:45:02 GMT
  
  [then HTML code]
  </code></pre>
  
  <p>Any help would be appreciated. Thanks.</p>