title: How to add a custom header to HttpClient()?
body: <p>I'm trying to add a custom header to the request header of my web application. In my web application im retrieving data from a web api, in this request i want to add a custom header which contains the string sessionID. I'm looking for a general solution so that I dont have to add the same code before every call I make.</p>
  
  <p>My Controller looks like this:</p>
  
  <pre><code>[HttpGet]
      public async Task&lt;ActionResult&gt; getCall()
      {
          string url = "http://localhost:51080/";
          string customerApi = "customer/1";
  
          using (var client = new HttpClient())
          {
              //get logged in userID
              HttpContext context = System.Web.HttpContext.Current;
              string sessionID = context.Session["userID"].ToString();
  
              //Create request and add headers
              client.BaseAddress = new Uri(url);
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              //Custom header
  
  
              //Response
              HttpResponseMessage response = await client.GetAsync(customerApi);
              if (response.IsSuccessStatusCode)
              {
                  string jsondata = await response.Content.ReadAsStringAsync();
                  return Content(jsondata, "application/json");
              }
              return Json(1, JsonRequestBehavior.AllowGet);
          }
      }
  </code></pre>
  
  <p>Hope anybody can help!</p>
  
  <p>Thanks in advance!</p>