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 –

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 and 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 :

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

Model class for REST GET service :

(3) Users.cs

Model class for REST GET service –

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

(4) UserResponse.cs

Model class for REST POST service :

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.





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