title: Using httpClient.postasync for web api calls .netcore
body: <p>I am new to .netcore, I am working on web api that are running on docker container and while using postman the web api's are working really fine outputting the results. I want to make a program in .netcore calling the webapi endpoints and getting the response and using that particular response in other endpoints with MVC.</p>
  <p>The explanation is given below.
  The default username and password for admin is default set for example <code>username:admin , password: helloworld</code>
  . The first time admin login the api requires a new personal password as shown in the Postman figure below.</p>
  <p>The login api is: <code>localhost://..../v1/users/login</code>
  <a href="https://i.stack.imgur.com/cLufG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cLufG.jpg" alt="Login Admin" /></a></p>
  <p>The first question is How to give the values in Authorization-&gt;BasicAuth using .netcore.
  The body of the api looks like the figure below.
  <a href="https://i.stack.imgur.com/Lz7Kv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lz7Kv.jpg" alt="Setting new_password for Admin" /></a></p>
  <p>After setting the new_password the response of the api is a token as given below.
  <a href="https://i.stack.imgur.com/1Ao1z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1Ao1z.jpg" alt="Token Generated" /></a></p>
  <p>The particular token is then use in the Environment to create user. The image for more clear problem is given below.
  <a href="https://i.stack.imgur.com/OmYrN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OmYrN.jpg" alt="Token Save" /></a></p>
  <p>Lastly, the token then used to make other API calls such as creating a user.
  <strong>API:</strong> <code>https://localhost/..../v1/users</code>
  The image is below.
  <a href="https://i.stack.imgur.com/52IgJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/52IgJ.jpg" alt="Creating a User" /></a></p>
  <p>As a newbie in .netcore language, I am really struggling to do this kind of API calls, as most of the tutorials I tried are generating their own token from API, but here I just want to take the response token and save it and then use it in other API calls.
  The StackOverflow community's support was always really handy for me.</p>
  <p>The Code I'm trying is given below.</p>
  <pre><code>**Controller**
  
   public class Login_AdminController : ControllerBase
      {
          [Route(&quot;/loginAdmin&quot;)]
          [HttpPost]
          public async Task&lt;string&gt; LoginAdminAsync([FromBody] dynamic content)
          {
              LoginAdmin L = new LoginAdmin();
              var client = new HttpClient();
              client.BaseAddress = new Uri(&quot;https://localhost:9090&quot;);
              var request = new HttpRequestMessage(HttpMethod.Post, &quot;/v1/users/login&quot;);
              var byteArray = new UTF8Encoding().GetBytes($&quot;&lt;{L.username}:{L.df_Password}&gt;&quot;);
              client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Basic&quot;, Convert.ToBase64String(byteArray));
  
              var formData = new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
              formData.Add(new KeyValuePair&lt;string, string&gt;(&quot;new_password&quot;, &quot;helloWorld123!&quot;));
  
              request.Content = new FormUrlEncodedContent(formData);
              var response = await client.SendAsync(request);
              Console.WriteLine(response);
              return content;
          }
     }
  }
  </code></pre>
  <pre><code>***Model***
      public class LoginAdmin
      {
          public string username = &quot;admin&quot;;
          public string df_Password = &quot;secret&quot;;
          public string new_Password { get; set; }
      }
  </code></pre>
  <p>Thank you.</p>