title: Setting Content type with Default Request Headers
body: <p>When using <code>HttpClient</code>, I have read examples (such as <a href="https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client" rel="nofollow noreferrer">this example</a>) that uses <code>DefaultRequestHeaders</code> to set the content type (such as "application/json") of a Post request.</p>
  
  <p>I tried to do something like this, but it failed. The <code>API</code> I am sending requests complained that it was sent an "unsupported type" (which it says when the content type is not set to <code>json</code>).</p>
  
  <p>After that I added one line and I solved the issue (you can see the line in the code below commented).</p>
  
  <p>My question is why is this line necessary? And if I include this line (that is setting the content type of the <em>content</em>) doesn't that make the "default request header" setting unnecessary. What is this "default request header" doing if anything?</p>
  
  <p>(I actually tried and commented the lines related to <code>DefaultRequestHeaders</code> and it worked without problem. So what is <code>DefaultRequestHeaders</code> good for?)</p>
  
  <p><strong>My code is :</strong></p>
  
  <pre><code>// Get the bytes for the request, should be pre-escaped
   byte[] bytes = Encoding.UTF8.GetBytes(jsonEmployeeData);
  
  client.BaseAddress = new Uri("the address here");
  //  client.DefaultRequestHeaders.Accept.Clear();
  client.DefaultRequestHeaders.Accept.Clear();
  client.DefaultRequestHeaders.Accept.Add(
         new MediaTypeWithQualityHeaderValue("application/json"));
  
  client.DefaultRequestHeaders.Add("customHeader", "blahblahblah");
  
  ByteArrayContent byteContent = new ByteArrayContent(bytes);  //Make a new instance of HttpContent (an abstract class that can't be instantiated)
  
  //THIS is the solution
  //byteContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");  //If I UNCOMMENT THIS, IT WORKS!!
  
  try
     {
    HttpResponseMessage response = await client.PostAsync("staff", byteContent);
    Console.WriteLine(response.ToString());
    }
   catch (Exception ex)
    {
   Console.WriteLine("Something happened, oopps!" + ex.Message);
    }
  Console.WriteLine("Press any key");
  Console.ReadLine();
  </code></pre>
  
  <p><code>client</code> is a <code>HttpClient</code> by the way.</p>