Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
10.8k views
in Programming by 66 69 85

I have RestAPI and it's Authorized by API Key and the Key must be adding in the HTTP request header, i tryed the below:

HttpClient client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("ApiKey", "myApiKey");

But still I get:

  • StatusCode: 401
  • ReasonPhrase: 'Unauthorized'

How to include the API-Key in my HTTP Request Header?


1 Answer

0 like 0 dislike
by 66 69 85
 
Best answer

How to add the API Key in the HTTP header?

For the API you are using you must add the key in the HTTP Header not in the Authorization.
Just you need to change the line below:

HttpClient client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("ApiKey", "myApiKey"));
If you don’t ask, the answer is always NO!
...