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 nuger.org. 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.

System.Net.Http.HttpClient

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.

RestSharp

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.

Satu tanggapan untuk “Cosuming JSON Rest API in C#

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

w

Connecting to %s