Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume REST in C# including PUT, POST and DELETE?

I have a REST webservice that I need to consume in C#. I need support for more than just GET requests though. I need everything that is done by REST including GET, PUT, POST, and DELETE. What is the best way of interfacing with this? I do not see anything for HTTPRequest to be able to do POST or anything other than GET unless you construct your own headers(which I prefer not to)

Is there some easy and lightweight way to fully consume REST webservices in C#?

like image 719
Earlz Avatar asked Mar 25 '10 17:03

Earlz


People also ask

How do I consume a REST service?

Right-click on the REST element and select Consume REST API.... In the displayed dialog, choose Add Single Method. Fill the information about the Method URL. You can include parameters between braces in the URL for the method's input parameters.

What is consume in REST?

Similarly, the act of consuming or using a REST API means to eat it all up. In context, it means to eat it, swallow it, and digest it — leaving any others in the pile exposed.

How do I send a REST API request?

The first REST API request in a session must be a sign-in request. This is a POST request that sends the user credentials in the body of the request. Because this is a POST request, the request must include the Content-Type header. You can send your the body of the request block as XML or JSON.


4 Answers

Check out the series of screencasts on WCF REST up on Channel 9:

http://channel9.msdn.com/tags/REST%20Starter%20Kit%20endpoint%20screencasts/

There's a great one amongst those called Consuming REST services with HttpClient which should give you a nice step-by-step instruction on how to do all of this.

Also check out the WCF REST Developer Center on MSDN for more articles, blog posts, and tutorial on WCF and REST.

like image 172
marc_s Avatar answered Sep 19 '22 02:09

marc_s


The HttpClient in the Microsoft.Http namespace that comes with the WCF Rest Starter kit is very helpful.

Aaron Skonnard from Pluralsight created several articles and screencasts highlighting the HttpClient.

There are several more resources that he posted to the Pluralsight blog in March 2009 covering the HttpClient.

like image 38
Brad Crandell Avatar answered Sep 20 '22 02:09

Brad Crandell


You could take a look at the REST Starter Kit on CodePlex.

like image 20
Darin Dimitrov Avatar answered Sep 20 '22 02:09

Darin Dimitrov


HTTPRequest is the request currently processed in a ASP app.

To make outboud HTTP REST requests, use the HttpWebRequest class. It has properties like Method (POST, PUT, DELETE) and you can write your payload into the request stream returned by GetRequestStream (or its async counterpart for high performance).

like image 31
Remus Rusanu Avatar answered Sep 23 '22 02:09

Remus Rusanu