HttpClient Techniques to consume APIs in minimal API .Net 6

Introduction

The different HttpClient techniques that we are going to explore are:

  • Register HttpClient Object Explicitly in Dependency Injection Service
  • Named Client
  • Type Client
  • HttpRequestMessage Object


Create a .Net6 Minimal API project

Let's create a .Net 6 Minimal API sample project to accomplish our demonstration. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any .Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor.


First of all, for this demo we can use the next command to create the minimal API project.



Create a third party API response Model


Here I'm going to use a free third-party rest API that is Json PlaceHolder. So, to receive the response let's create a response model like Post.cs.


Register HttpClient Object Explicitly in DI

Initialize the HttpClient object with a configuration like 'domain', 'default headers', etc in the Dependency injection services. After that, we can pass the HttpClient object as an input parameter to the delegate handler of our Minimal API endpoint. The only negative impact of this approach is when we have multiple different domain APIS to consume this approach fails to work. Because the last HttpClient object registration will override all other HttpClient registration. So this technique is only good when our application consumes a single third-party API domain.

Let's initialize and register the HttpClient in DI.


  • Here on HttpClient instance register the Base Address value that is the domain name of the 3rd party API.

Now we have to add a minimal API GET endpoint to consume this 3rd party API.


  • So here we can observe our minimal API endpoint delegate handler method has input parameter of type HttpClient. So instance of HttpClient created by the framework with the help of IHttpClientFactory.


Named Client

The HttpClient object will be generated or invoked with the names. So while registering the client we have to specify the name for the HttpClient. Using the same name we can get the instance of HttpClient from the IHttpClientFactory. This approach supports multiple HttpClient registrations with their respective registered names.



  • Here we registered HttpClient instance with a name('jsonplaceholder'). This same name will be used by the IHttpClientFactory to create the HttpClient instance.

Now let's create a minimal endpoint that uses the 'Named Client' technique to consume the third-party APIs.


Using the registered name('jsonplaceholder') we are creating the HttpClient instance from the IHttpClientFactory.


Typed Client

Each external API domain will have a separate class to inject the HttpClient object. This class will be registered as a type of HttpClient in the DI services.

So let's create a typed class that is specific to a domain like PostHttpClient.cs.


  • Here our PostHttpClient typed class is specific to domain ("jsonplaceholder.typicode.com"). So API calls related domain will be implemented in this class.

Now register our type 'PostHttpClient' in DI.



Now let's create a minimal API endpoint to consume the external API using a typed client.


  • Here we can observe our typed client(PostHttpClient) is passed as an input parameter to the delegate handler of our endpoint.


HttpRequestMessage Object

In this approach HttpRequestMessage object will be used to configure settings like domain, headers, payload, etc, this object will be used by the HttpClient object to invoke or consume the rest API.

Here just register the plain AddHttpClient service.

Now let's add our minimal API endpoint to consume the third-party API using the HttpRequestMessage object configuration.


So here we can observe all the API-related configurations are taken place in the HttpRequestMessage object.

Using IHttpClientFactory generates the HttpClient instance.

Using the HttpClient.SendAsnync method invoke the API by passing the HttpRequestMessage object as an input parameter.



Wrapping Up


Hopefully, I think this post delivered some usefull information on implementing different HttpCLient techniques to consume api in minimal API. I love to have your feedback, suggestions, and better techniques in the comment section, if you like this please share with others.

Share this post