I am new to .net-core and docker. The problem I am going to explain here is a bit tricky, but if you have any type of questions please do ask me and I will explain it more clearly.

So, I have a docker container running on my localhost for whatsapp business api, as explained in Whatsapp Business Api Documentation.

I have already downloaded Postman Collection of Whatsapp Business Api from Whatsapp Business Postman Collection.

Everything seems to be working perfectly fine. But now I want to call these pre-built apis with .net-core and get the responses. As I couldn't find anything good, or also as a newbie it's difficult for me to understand. So for some knowledge I would like to know How can I call them and get the effective response. For example, take an example of a post request for admin login

API: {{URL}}/v1/users/login Authorization -> (Basic Auth) username: admin password: **** 
RESPONSE: {     "users": [         {             "token": "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJ1c2VyIjoiYWRtaW4iLCJpYXQiOjE1NTk5MTE1NTUsImV4cCI6MTU2MDUxNjM1NSwid2E6cmFuZCI6LTQxMzgyNDE0MjYwMDI5NjA1OTl9.PYAhEilXX3GDhRo0O-M0V0aXWfL5THJkG65LfIesxh4",             "expires_after": "2019-06-14 12:45:55+00:00"         }     ],     "meta": {         "version": "v2.23.4",         "api_status": "stable"     } } 

the images for more explanation can be seen below in the request and response links.

The Login Authentication UI Image.

The Login Authentication Postman Image.

The Login Authentication Response New Password UI Image.

The Login Authentication Response Postman Image.

Now with this token I can create more users by other api calls. How can I call an api like this and get the response with token and use that token for more functionalities or api calls with .netcore.

Thank you.

Tag:rest, asp.net-core, asp.net-core-mvc

2 comments.

  1. Chris Pratt

    Your question is very broad, but in general, you want to create a service class where you inject a typed HttpClient instance (see: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#typed-clients).

    The token can be stored on a private field or property in that service class. Then, just create a private method like GetTokenAsync in the service class, where you will either get the value from the field, or if it's null, then call the API to get it. All your other methods will then call GetTokenAsync to get the token they need, and make their own calls. Finally, inject the service class where you need it.

    1. Farrukh Ahmed Khan

      Thank you for your answer. Can you give me a link or a piece of snippet where I can understand this more clearly, How to do it .netcore.

Add a new comment.