Stephen Melben Corral
Stephen Melben Corral

Reputation: 319

How to add api key in request header using web api

Hi guys this is my first time using web api and I hope you can point me in the right direction. How do I add the api key in request header using web api?

I tried to check google but i'm not sure if I'm looking at the right guide. This is what I found > How to add and get Header values in WebApi

My goal is to make a GET request and add the API key in the request headers.

Upvotes: 6

Views: 63007

Answers (3)

Raghvender Kataria
Raghvender Kataria

Reputation: 1485

You always have key-value pair in header of any API request. For example here you have the header with key as "api_key" and value as "1234". You can add this in your Http request by the way given below.

HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri("Your_get_URI");
request.Method = HttpMethod.Get;
request.Headers.Add("api_key", "1234");
HttpResponseMessage response =  await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var statusCode = response.StatusCode;

Upvotes: 26

Anthony G.
Anthony G.

Reputation: 662

If you are using DI you could easily inject a configured HttpClient by doing a little setup in Startup.cs

The following is a working example of configuring a HttpClient for use with Microsoft's App Insights api. Of course you will have to change your header as needed.

public void ConfigureServices(IServiceCollection services)
{
    //Somewhere in the ConfigureSerices method.
    services.AddHttpClient("APPINSIGHTS_CLIENT", c => 
    {
        c.BaseAddress = "<API_URL_HERE>";
        c.DefaultRequestHeaders.Add("x-api-key", clientKey));
    }
}

Now if you inject IHttpClientFactory for use downstream, and call it will be configured and ready to be used without any fuss.

HttpClient client = factory.CreateClient("APPINSIGHTS_CLIENT"); 

Upvotes: 6

Akhatarali Ansari
Akhatarali Ansari

Reputation: 70

Try this, I hope this will work for you.

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri("API URL");
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Authorization = new
                    System.Net.Http.Headers.AuthenticationHeaderValue("Pass your token value or API key");
                HttpResponseMessage response = await httpClient.GetAsync(endpoint);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    if (string.IsNullOrEmpty(result))
                        return "Success";
                    else
                        return result;
                }
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    throw new Exception(await response.Content.ReadAsStringAsync());
                }
            }

Upvotes: -1

Related Questions