perlwhite
asked on
C# REST PUT
Can someone provide me a very basic example of how to use C# to submit a PUT REST request?
ASKER
I do not control the service. I have a URL and list of parameters to be passed. All I want to do is insert/update records using PUT/POST. I am able to do GET.
how are you performing GET? Please post your code.
ASKER
Here is my GET code:
HttpWebRequest myRequest = (HttpWebRequest)WebRequest .Create("/ /url");
myRequest.Headers.Add("//k ey", "//value");
WebResponse response = myRequest.GetResponse();
Stream responseData = response.GetResponseStream ();
StreamReader reader = new StreamReader(responseData) ;
string responseFromServer = reader.ReadToEnd();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest
myRequest.Headers.Add("//k
WebResponse response = myRequest.GetResponse();
Stream responseData = response.GetResponseStream
StreamReader reader = new StreamReader(responseData)
string responseFromServer = reader.ReadToEnd();
ASKER
The codeproject article talks about HTTP POST. I am trying to do PUT using REST.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The API documentation asks me to send some required parameters and they show their sample input in json
ok, creating a JSON string is another topic and deserves another Question.
As a summary, you may need to use the JavaScriptSerializer class. Some tutorial here:
http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx
Once you have your JSON string, use the suggested code with mime type "application/json"
As a summary, you may need to use the JavaScriptSerializer class. Some tutorial here:
http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx
Once you have your JSON string, use the suggested code with mime type "application/json"
Open in new window
ServiceLibrary IHelloWorldService.cs
Open in new window
ServiceHost Program.cs
Open in new window
ServiceHost App.config
Add the following between the <configuration></configura
Open in new window
ServiceClient Program.cs
Open in new window
ServiceHost and ServiceClient both require a reference to the ServiceLibrary project. Also all projects will require additional references to System.ServiceModel and System.ServiceModel.Web.
You will most likely need to run Visual Studio as an administrator as access to ports is usually restricted, so running this as a non administrator will throw an access denied error in the ServiceHost when attempting to open the port.