Answered by:
If REST is so simple...

Question
-
User-2134351927 posted
If POSTing to REST services is so simple then why is can't ai find a single simple example? If I have an end-point that looks something like this:
Service 1- http://thedata.is.here.com:8070/restservice/restservice/status
3 Sample test data-
{"number" : "123456789", "type" : "ORDER"}
I could do this in SOAP with my eyes closed! Am I making this more diffacult then it is?
Cheers, thanks for any help!
Monday, November 11, 2013 4:52 PM
Answers
-
User260886948 posted
Hi,
When occur the above problem, please try to check whether the binding or even the entire <service> configuration element has been correctly applied to the client or service. Sometimes, the configuration might not be correctly applied due to some type mistakes in the configuration file(on the name attribute...).
Here are some web reference and former thread that mentioned this kind of issue:#Fixing the WCF 'The remote server returned an error: (415) Unsupported Media Type.HTTP GET' Error
http://allen-conway-dotnet.blogspot.com/2010/06/fixing-wcf-remote-server-returned-error.htmlAnd the WCF tracing is always a useful tool that can help you find certain exception or error in the service. You can turn it on in your WCF servcie:
#Configuring Tracing
http://msdn.microsoft.com/en-us/library/ms732023.aspxhttp://msdn.microsoft.com/en-us/library/ms733025.aspx
Also use Fiddler or Wcf log to see how your client request looks like.
#Fiddler:
http://fiddler2.com/ .Best Regards,
Amy Peng- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 3:55 AM
All replies
-
User-488622176 posted
Depends on the service. Please check these samples:
- http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html
- http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp
- http://msdn.microsoft.com/en-us/library/jj819168.aspx
- http://stackoverflow.com/questions/14446019/passing-complex-data-to-rest-wcf-post-method
- http://social.msdn.microsoft.com/Forums/vstudio/en-US/c24e3957-5440-4fd3-a2c7-97ef7c039d16/wcf-rest-service-with-an-array-of-a-complex-object-in-wrapped-request?forum=wcf
If you search google on C# REST "complex object" you get several other samples.
Tuesday, November 12, 2013 8:13 AM -
User-2134351927 posted
Thanks for the reply, I've looked at these and have spent hours pulling them apart and trying to make them work. Unfortunatly no joy... Regardless of what I do I get
" Error 415 Unsupported Media Type "
public void btnSubmit_Click(object sender, EventArgs e) { if (Page.IsValid) { try { string[] fP1 = fPRAM1.Text.Split(','); string[] fP1V = fPRAM1V.Text.Split(','); string[] fP2 = fPRAM2.Text.Split(','); string[] fP2V = fPRAM2V.Text.Split(','); lbMessageDialog.Text = HttpPost(fURL.Text, fP1, fP1V, fP2, fP2V).ToString(); } catch (Exception ec) { lbMessageDialog.Text = "Problem Saving Record..." + ec.Message.ToString(); } finally { } } } static string HttpPost(string url, string[] paramName, string[] paramVal, string[] paramName1, string[] paramVal1) { HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest; req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; // Build a string with all the params, properly encoded. // We assume that the arrays paramName and paramVal are // of equal length: StringBuilder paramz = new StringBuilder(); for (int i = 0; i < paramName.Length; i++) { paramz.Append(paramName[i]); paramz.Append("="); paramz.Append(HttpUtility.UrlEncode(paramVal[i])); paramz.Append("&"); paramz.Append(paramName1[i]); paramz.Append("="); paramz.Append(HttpUtility.UrlEncode(paramVal1[i])); paramz.Append("&"); } // Encode the parameters as form data: byte[] formData = UTF8Encoding.UTF8.GetBytes(paramz.ToString()); req.ContentLength = formData.Length; // Send the request: using (Stream post = req.GetRequestStream()) { post.Write(formData, 0, formData.Length); } // Pick up the response: string result = null; using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(resp.GetResponseStream()); result = reader.ReadToEnd(); } return result; }
Any help greatly welcome!
Tuesday, November 12, 2013 11:05 AM -
User-488622176 posted
Did you configure your endpoint correctly?
See this post : http://stackoverflow.com/questions/16792202/error-415-when-posting-json-data-to-wcf-service
Thursday, November 14, 2013 3:29 AM -
User260886948 posted
Hi,
When occur the above problem, please try to check whether the binding or even the entire <service> configuration element has been correctly applied to the client or service. Sometimes, the configuration might not be correctly applied due to some type mistakes in the configuration file(on the name attribute...).
Here are some web reference and former thread that mentioned this kind of issue:#Fixing the WCF 'The remote server returned an error: (415) Unsupported Media Type.HTTP GET' Error
http://allen-conway-dotnet.blogspot.com/2010/06/fixing-wcf-remote-server-returned-error.htmlAnd the WCF tracing is always a useful tool that can help you find certain exception or error in the service. You can turn it on in your WCF servcie:
#Configuring Tracing
http://msdn.microsoft.com/en-us/library/ms732023.aspxhttp://msdn.microsoft.com/en-us/library/ms733025.aspx
Also use Fiddler or Wcf log to see how your client request looks like.
#Fiddler:
http://fiddler2.com/ .Best Regards,
Amy Peng- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 3:55 AM