Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct URI for sending parameters via POST in WCF REST Services?
    primarykey
    data
    text
    <p>Let's say that I have specified the following WCF REST Service at the address "<a href="http://localhost/MyRESTService/MyRESTService.svc" rel="nofollow noreferrer">http://localhost/MyRESTService/MyRESTService.svc</a>"</p> <pre><code>[ServiceContract] public interface IMyRESTService { [OperationContract] [WebInvoke( Method = "POST", UriTemplate = "/receive")] string Receive(string text); </code></pre> <p>Now I can call my REST service in Fiddler using the address "<a href="http://localhost/MyRESTService/MyRESTService.svc/receive" rel="nofollow noreferrer">http://localhost/MyRESTService/MyRESTService.svc/receive</a>" and it works (I'll get a return value).</p> <p>But what if I want to send parameters to my REST Service? Should I change my interface definition to look like this:</p> <pre><code>[ServiceContract] public interface IMyRESTService { [OperationContract] [WebInvoke( Method = "POST", UriTemplate = "/receive/{text}")] string Receive(string text); </code></pre> <p>Now if I'll call the REST Service in Fiddler using the address "<a href="http://localhost/MyRESTService/MyRESTService.svc/receive/mytext" rel="nofollow noreferrer">http://localhost/MyRESTService/MyRESTService.svc/receive/mytext</a>" it works (it sends the parameter "mytext" and I'll get a return value). So is this the correct URI for sending parameters via POST?</p> <p><strong>What confuses me is that I don't know how to use this URI exactly in code at the same time when I'm sending parameters</strong>. I have this following code which is almost complete for sending POST data to a WCF REST Service but I'm in stuck with how to take parameters into account with URI. </p> <pre><code>Dictionary&lt;string, string&gt; postDataDictionary = new Dictionary&lt;string, string&gt;(); postDataDictionary.Add("text", "mytext"); string postData = ""; foreach (KeyValuePair&lt;string, string&gt; kvp in postDataDictionary) { postData += string.Format("{0}={1}&amp;", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value)); } postData = postData.Remove(postData.Length - 1); Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive"); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); req.Method = "POST"; byte[] postArray = Encoding.UTF8.GetBytes(postData); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = postArray.Length; Stream dataStream = req.GetRequestStream(); dataStream.Write(postArray, 0, postArray.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)req.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string responseString = reader.ReadToEnd(); reader.Close(); responseStream.Close(); response.Close(); </code></pre> <p>If I'll want to send parameters (e.g. "mytext") in code via POST should the URI code be either</p> <p>this</p> <pre><code>Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive"); </code></pre> <p>or this (this works but it doesn't make any sense since parameters should be added other way and not directly to the URI address)</p> <pre><code>Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive/mytext"); </code></pre> <p>I'm glad if you can help me, it can't be so difficult with WCF REST Services. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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