Click here to Skip to main content
16,019,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have an external app that has an Web API/Service to use from our internal software or different apps.

Anywho I just can't seem to make the HttpClient work. It works with the older WebClient, or with RestClient among other tools.I just can'y make it work with HttpClient and it really bugs me, as in I don;t know why. The IP and API key are fake.

So with RestClient and other techonlogies it works:

C#
public async Task<List<Employee>> LoadEmployees()
        {
            var client = new estClient("http://99.99.99.99:8080/api/v2/employee/");
            var request = new RestRequest(Method.GET);
            request.AddHeader("cache-control", "no-cache");                     
            request.AddHeader("accept-encoding", "gzip, deflate");
            request.AddHeader("Host", "99.99.99.99:8080");           
            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Accept", "*/*");           
            request.AddHeader("Authorization", "apwerfhafdh>0923817adfhhasfd<9");
            request.AddHeader("Content-Type", "application/json");
            IRestResponse responseData =  await client.ExecuteGetTaskAsync(request);

            if (responseData.StatusCode == System.Net.HttpStatusCode.OK)
            {                
                List<Employee> emps = JsonConvert.DeserializeObject<List<Employee>>(responseData.Content);

                return emps;
            }
            else
            {
                throw new Exception($"{responseData.StatusCode} : {responseData.StatusDescription}");
            }            
        }


but with HttpClient I can't manage to make the Authorization part work.

What I have tried:

C#
Client= new HttpClient();
            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));            
            Client.DefaultRequestHeaders.Add("Accept", "*/*");
            Client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate");

            Client.DefaultRequestHeaders.Add("Authorization", "X-ApiKey apwerfhafdh>0923817adfhhasfd<9"); // I get Unathorized
//Client.DefaultRequestHeaders.Add("Authorization", "apwerfhafdh>0923817adfhhasfd<9"); // I get System.FormatException

            //CLient.DefaultRequestHeaders.Add("X-ApiKey", "apwerfhafdh>0923817adfhhasfd<9");   I get Unauthorized  
//Client       
Posted
Updated 5-Jun-19 15:02pm
Comments
F-ES Sitecore 1-Jun-19 7:01am    
You're setting the Authorization header to be different things. In one it is just the key, in the other you have X-Apikey before it.

If I understand it correctly, your API only accepts the exact string apwerfhafdh>0923817adfhhasfd<9 as Authorization header. Why HttpClient rejects that exact value with a FormatException, is because it is an invalid Authorization value: valid Authorization values are of the format [type] [credentials], so like your X-ApiKey code format. If the API gives Unauthorized, it means that the API only accepts strictly invalid Authorization values, so either check if the API really supports no valid type or tell whoever wrote the API to fix this issue and make sure that there is some valid Authorization header that's accepted.
 
Share this answer
 
v5
Comments
danyDude 1-Jun-19 12:33pm    
Yes, you are right. It only accepts apikey after authorization. Not [authtype] [credentials] but just [credentials]. Thanks
If you can't fix the API, the workaround is to call a different method to add the header, as mentioned in this StackOverflow answer:
c# - Adding HttpClient headers generates a FormatException with some values - Stack Overflow[^]
HttpHeaders.TryAddWithoutValidation Method (System.Net.Http.Headers) | Microsoft Docs[^]
C#
Client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "apwerfhafdh>0923817adfhhasfd<9");
 
Share this answer
 
v2
Comments
danyDude 8-Jun-19 12:19pm    
Thanks Richard. I managed it using the RestSharp nuget package/library. But is cool to see how to make it work using HttpClient. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900