Home > OS >  How can i get Headers value with Net Core?
How can i get Headers value with Net Core?

Time:01-29

I send a request on the API, the request and the response are successful, but I want to get the value equivalent to the authentication keyword through the response. How can I do that? I tried this way on the examples I found, but it doesn't give any results .Net 6.0

using LoggerApi.Login;
using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using System.Linq;
using Microsoft.Extensions.Primitives;

namespace LoggerApi.Login
{
    public class AdminLogin
    {
        public async static Task<object> GetAuthenticationCode()
        {
            var client = new HttpClient();
            var loginEndpoint = new Uri("https://admin.com/login");
            var loginPayload = new LoginPayload()
            {
                Username = "admin",
                Password= "admin",
            };
            
            var requestJson = JsonConvert.SerializeObject(loginPayload);
            var payload = new StringContent(requestJson, Encoding.UTF8, "application/json");
            var res = await client.PostAsync(loginEndpoint, payload).Result.Headers.TryGetValues("authentication");
            
            return res;
           
          
      


        }
    }
}

CodePudding user response:

One way to get the value equivalent to the authentication keyword through the response is to check the headers of the response. In the given code, the headers are accessed via the Headers property of the HttpResponseMessage object returned by the PostAsync method. The method TryGetValues("authentication") is used to attempt to retrieve the value associated with the "authentication" key from the headers.

If this method returns false, it means that the "authentication" key is not present in the headers, so you can check if the key is present using the Contains method and if it is present then you can get the value using the GetValues method.

Another way to check the value of authentication keyword is to check the content of the response, you can deserialize the json content of the response using the JsonConvert.DeserializeObject method, then you can access the value of the authentication keyword by using the dot notation.

It is also possible that the server returns the authentication code in the body of the response instead of the headers, so you can check the content of the response by calling the Content.ReadAsStringAsync() method on the HttpResponseMessage object, and then deserializing the json string into an object, then you can access the value of the authentication keyword by using the dot notation.

Here is an updated version of the GetAuthenticationCode method that should work:

public async static Task<string> GetAuthenticationCode()
{
    var client = new HttpClient();
    var loginEndpoint = new Uri("https://admin.com/login");
    var loginPayload = new LoginPayload()
    {
        Username = "admin",
        Password= "admin",
    };

    var requestJson = JsonConvert.SerializeObject(loginPayload);
    var payload = new StringContent(requestJson, Encoding.UTF8, "application/json");
    var response = await client.PostAsync(loginEndpoint, payload);

    if (response.IsSuccessStatusCode)
    {
        // Check headers for authentication code
        if (response.Headers.TryGetValues("authentication", out var authValues))
        {
            return authValues.FirstOrDefault();
        }

        // Check body for authentication code
        var jsonString = await response.Content.ReadAsStringAsync();
        var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
        if (jsonObject.ContainsKey("authentication"))
        {
            return jsonObject["authentication"];
        }
    }

    return null;
}
  • Related