Generic REST Client in C#

Share this

In this tutorial, I will create a generic REST client in C# for consuming REST services of various types like GET, POST etc supporting basic authentication and passing custom HTTP headers and receive response asynchronously. I have build this sample using .NET Core 2.2 which will work equally well with .NET Framework as no specific libraries targeting .NET Core has been used.

Rest Client is created as a Generic Asynchronous method. I have also used a Constraint with this Generic class that only a class can be used as a placeholder type for the Generic Rest Client class. I have imposed a constraint of class type to the Generic class as the REST api response will the cast to the specific model type that I will define.

Using this REST Client, I will demonstrate the following: –

  • How to call REST services supporting Http verbs like GET and POST. You can try out services supporting other Http verbs
  • You will also be able to use basic authentication providing username and password to authenticate the service
  • You will also be able to set a HTTP Header value. You can easily customize the REST client to set multiple HTTP Header values

You can visit one of my previous article JSON Parsing in C# With this method, you can parse the REST response in a non generic way.

Source Code : Complete source code can be downloaded from my GitHub repo –

https://github.com/sudipta-chaudhari/GenericRestClient_CSharp

In this article, I will demonstrate this by creating a .NET Core Console application in which I will consume few sample fake REST services hosted on internet. The list of services I will consume are as follows: –

You can visit the sites https://jsonplaceholder.typicode.com/ and https://reqres.in/ available at the time of writing this article. These site provide fake REST service endpoints which you can consume for your development and get a response.

I have created model classes to hold the response returned by these services. The model classes are listed as follows in the same order as the services listed above.

(1) ToDo.cs

Model class for REST GET service : https://jsonplaceholder.typicode.com/todos/1

(2) No separate model class, returns IList<ToDo>

Model class for REST GET service :https://jsonplaceholder.typicode.com/todos

(3) Users.cs

Model class for REST GET service – https://reqres.in/api/users?page=2

This class file contains two classes named – Users and Data as follows.

(4) UserResponse.cs

Model class for REST POST service : https://reqres.in/api/users

The code for RestClient can be seen as follows.

This class is a generic class having a constraint of type class imposed on this as mentioned at the beginning of this article. HttpWebRequest class is used to consume api and fetch response. Newtonsoft.Json is used to Serialize request object as JSON and write to request body. StreamReader class is used to read the response. Finally, the response is Deserialized to object of the generic type passed in and is returned.

Finally, code to consume the rest services can be seen as follows.

Please see the following screenshots while debugging which consume the REST services mentioned above with the response received and deserialized into the corresponding model class object.

GenericRESTClient_CSharp_Response_GET_1

GenericRESTClient_CSharp_Response_GET_2

GenericRESTClient_CSharp_Response_GET_3

GenericRESTClient_CSharp_Response_POST_1

This concludes the article – Generic REST Client in C#.

I hope you liked this article and would like to receive your feedback. If you have any comments, questions or suggestions, please post them in the comments section below this article. I will try to respond at my earliest or somebody else reading the article and your comment will try to respond.


Share this

Leave a Reply