Background
There are numerous ways to consume REST API in C#. Starting with HttpClient, WebClient, HTTPWebRequest and Third-Party API. On this post, I will show you how to consume REST API in an easy way with C#.
Requirements
For this lab we use:
Visual Studio 2019 – http://visualstudio.com
Newton soft JSON - https://www.newtonsoft.com/json/
Rest sharp - http://restsharp.org/
Horoscope REST API sample – http://sandipbgt.com/theastrologer/api/horoscope
//
Let's get started
#1 Creating a project with Visual Studio 2019
we created two projects the first one is Class Library and the second one is Console App. It used .NET Framework 4.7.2.
we downloaded two Nuget package: Newtonsoft JSON and RestSharp for the Class Library project
#2 Composing Classes for our REST objects
The REST API will be consumed as an object. Therefore, we created several classes and enumeration
#3 Creating Class that Consumes the REST API
We created a class that has a method to consume REST API. We deserialize the REST API as shown below. We read the content and save the result into Zodiacresult Class. Please note we use DeserializeObject (from newtonsoft) and RestClient (from restsharp).
#4 Creating Class that Display the REST API
We created a console app that consume the library. The REST API has a nested result. Therefore, the Zodiacresult contains an array namely Meta object. We consume the Meta object and accessing through their properties.
#5 Testing the result
Lets push the F5 button and you can see, we can consume the rest API easily in dotnet.
Have a question? Just put it in the comments. Enjoy your weekend.
//
Hi Folks, today i will share about how to consume REST Web Based API in .NET. This post will resume the entire way to consume REST API from Web, Desktop, and Xamarin. Let's get started.
Why the REST web API important?
Because the REST based API replaces the previous techhnology such as Web Services, Remoting, and common TCP/IP communication. the simplification of the REST gives developer more time to work with the business process rather than work for improving the communication between system. This post won't discuss about how to design the REST API but how to consume it. If you want to learn about the design principles please go here . Nowadays, many software communicate each other with Rest web Based API. For example, Twitter, Microsoft Graph, Facebook, and many more. This post become fundamental for developer who want to consume the API for their application
Considering the Access
In general, you have two ways to consume REST based API. Parsing or Wrapping. Parsing is done by understanding the XML or JSON request and then read the response by using interpreter of JSON or XML. A good example of parsing method is Graph Explorer. You can see the example here http://dapalan.com/LdFh. Wrapping is using ready to use API that help specific developer to acomplish the mission. For example, is Graph API SDK
When to use parsing method
- You want to reuse the code in similar environment such as consuming console app, desktop app, and UWP app all together without considering the different SDK
- You want faster and smoother experience when the API changes. The drawback of the Wrapping method is depend with the supported API, so when the API changes you should reconfigure the wrapper
//
When to use wrapping method
- you dont understand how to parse, split, and read JSON or XML syntax and operation
- you need faster way to develop the solution without thinking the detail
Consuming Strategy
Option A. Using Generic Way (Parsing)
I recommend you to learn by using Newtonsoft JSON. They have 100 codes sample to help you to understand how to serialize and de-serialize API that use JSON
Option B. Using Specific API (Wrapping)
This is recommended way, if your codes is supported by the API maker. For example Graph Explorer SDK, and others.
In the next post we will show you some sample codes to do that.
//