RestSharp – Simple REST and HTTP API Client for .NET

I would like to present you a simple .NET client for REST and HTTP API – RestSharp.

RestSharp is a .NET client for REST and HTTP API. To use RestSharp you need to install it by NuGet:

I also use Newtonsoft.Json to deserialize/serialize data:

I found test REST API https://jsonplaceholder.typicode.com/ to test RestSharp. I’ll try to get albums for that test REST API. I need a album class:
[csharp]
using Newtonsoft.Json;

namespace Letys.RestApiClient.Domain
{
[JsonObject(MemberSerialization.OptIn)]
public class Album
{
[JsonProperty(“id”)]
public int Id { get; set; }

[JsonProperty(“title”)]
public string Title { get; set; }
}
}
[/csharp]
Now, we can create client to connect with test REST API:
[csharp]
using Letys.RestApiClient.Domain;
using Newtonsoft.Json;
using RestSharp;
using System.Collections.Generic;

namespace Letys.RestApiClient
{
public class Client
{
private RestClient client;

public Client(string url)
{
this.client = new RestClient(url);
}

public List GetAlbums()
{
var request = new RestRequest(“albums”, Method.GET);

IRestResponse response = client.Execute(request);
var content = response.Content;

return JsonConvert.DeserializeObject>(response.Content);
}

public Album GetAlbum(string id)
{
var request = new RestRequest(“albums/{id}”, Method.GET);
request.AddUrlSegment(“id”, id);

IRestResponse response = client.Execute(request);
var content = response.Content;

return JsonConvert.DeserializeObject(response.Content);
}
}
}
[/csharp]
So, this is a simple example of using RestSharp 😉 Please read more about RestSharp here.

I found great article about RESTful Web Services. You can find more about REST services there.