J4N J4N - 1 year ago 118
reST (reStructuredText) Question

How to interact with a REST service in C#

I'm developing a small application that has to interact with a JSON/REST service.
What is the easiest option to interact with it in my c# application.

I don't need to have the best performances, since it's just a tools that will do some synchronization once a day, I'm more oriented toward the ease of use and the time of development.

(the service in question will be our local JIRA instance).

Answer Source

I think the best way by far is to use RestSharp. It's a free Nuget Package that you can reference. It's very easy to use and this is the example from their website:

var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)

// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string

// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;

// easy async support
client.ExecuteAsync(request, response => {

// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {

// abort the request on demand
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download