Cosuming JSON Rest API in C#

There is a lof of good libraries to consume JSON API in C#, we can search for rest client in First item on the search result is RestSharp,  a simple REST and Http API client. You can also use System.Net.Http.HttpClient as it is a base for sending http requests and receiving http responses provided by default on .net framework and .net core. In this post, i’ll show both ways how to cosume JSON Rest API.


Make an instance of HttpClient, set the baseAddress and set appropriate headers as required by the Rest API that we’re consuming. We expect the returned response in JSON Format but since the response was escaped string with backslashes so we need to clear he escaped string content then parse it into proper JSON Object.


If you use Visual Studio then open nuget package manager console type install-package RestSharp or use the gui by right-click on the project and select manage Nuget Package and click install. But if you use dotnet cli, type dotnet add package RestSharp in the terminal.

In my opinion RestSharp provide the most simple mechanism in making Http Request and receiving the Response with easy async support. It also has builtin Json serializer, therefore no need to rely on other Json serializer to parse the Http Response.
Full source code is here.