REST Api always return 401 status code when i am calling API by HttpClient

T.Zacks 3,996 Reputation points
2022-08-10T10:59:50.857+00:00

I am working with Nasdaq Fund Network Data Service first time. i am calling their one of the API where passing user id,pwd and access key but always getting 401 status code. i am not able to figure out what is wrong in my http call. please some one have a look at the code and tell me where i made the mistake for which i am getting 401 status code instead of right response.

here is my sample code where i could not share actual credentials and access key.

string url = "https://nfn.nasdaq.com/servicecall/tempsession";  
Uri u = new Uri(url);  
  
string username = "test1";  
string password = "test1";  
string accessKey = "my key";  
  
var payload = new Dictionary<string, string>  
{  
  {"username", username},  
  {"password", password},  
  { "accesskey", accessKey}  
};  
  
string strPayload = JsonConvert.SerializeObject(payload);  
HttpContent c = new StringContent(strPayload, Encoding.UTF8, "application/x-www-form-urlencoded");  
  
var response = string.Empty;  
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;  
using (var client = new HttpClient())  
{  
 //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessKey);  
 //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "Bearer " + accessKey);  
  
 HttpRequestMessage request = new HttpRequestMessage  
 {  
 Method = HttpMethod.Post,  
 RequestUri = u,  
 Content = c  
 };  
  
 var result = client.SendAsync(request).Result;  
 if (result.IsSuccessStatusCode)  
 {  
 response = result.StatusCode.ToString();  
 }  
}  

various way i tried to get response but always getting 401 status code. just do not understand what is wrong in the code. please help me. thanks

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-08-10T13:53:07.487+00:00

    please see my latest code where i am using application/x-www-form-urlencoded but still getting same 401 status code. looking for next guidance.

    I'm a little confused by your response as it does not follow the coding pattern in the link. I created a application/x-www-form-urlencoded test with HttpClient and it works as expected.

    static async Task ExecuteFormPost()  
    {  
        string url = "https://localhost:44367/servicecall/tempsession";  
        var postParamters = new Dictionary<string, string>();  
        postParamters.Add("username", "username");  
        postParamters.Add("password", "password");  
        postParamters.Add("accesskey", "accesskey");  
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(postParamters) };  
        HttpResponseMessage response = await client.SendAsync(request);  
      
        string text = await response.Content.ReadAsStringAsync();  
        Console.WriteLine(text);  
      
    }  
    

    I can't test the actual service as I do not have an account.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.