Consuming RESTful web services

Until now, we have created RESTful services and discussed server-side code with the help of code examples. We have consumed these services using external third-party tools, such as Postman and Advanced RESTClient. We have also consumed these services using fake objects and during unit testing. While helpful, these examples of consumption have not shown the true strength of RESTful services, as they have either tested their functionality or verified its output.

There may be situations where you will need to consume or use these services within another application similar to a controller, or even your own application itself. These applications can be any of the following:

Let's take a look at one of the applications we've already discussed: let's say you need some mechanism to implement or consume an external API (in this case, PayPal) while integrating an online payment system. In this case, the external tools we've already covered, such as Postman and Advanced RESTClient can’t help; to meet your needs, you require a REST client.

The following diagram illustrates how services can be consumed using a REST client with the help of an HTTP client. In the following diagram, the REST client is interacting (request, response) with both external and network services that have been developed in ASP.NET Core or are located on either the same or a different server.

Web, Console, Mobile, and so on, are the clients consuming these services with the help of the REST client.

We will now discuss how to build a REST client that we can use to consume other RESTful web services (that is, APIs) in our application.