title: Unsupported Media Type with HttpClient / .NET Core
body: <p>I am working with a RESTful API that supports POST requests in JSON format. The API's own Swagger documentation shows that this is a valid call to one of its endpoints: </p>
  
  <pre><code>curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '&lt;JSON&gt;' '&lt;URL&gt;'
  </code></pre>
  
  <p>Where <code>&lt;JSON&gt;</code> and <code>&lt;URL&gt;</code> are the valid JSON message and the endpoint's URL. From this Swagger documentation I gather that any posts to this endpoint must include both a <code>Content-Type</code> and <code>Accept</code> headers set to <code>application/json</code>.</p>
  
  <p>I am writing a C# method that will use .NET Core's <code>HttpClient</code> class to post to this endpoint. However, upon posting my message I receive an HTTP 415 error code back, for Unsupported Media Type. From what I've learned so far, the <code>Content-Type</code> header must be set in your content (I am using the <code>StringContent</code> class) and the <code>Accept</code> header can only be set in the <code>HttpClient</code> headers. Here is my particular example: </p>
  
  <pre><code>var httpContent = new StringContent("&lt;JSON&gt;", Encoding.UTF32, "application/json");
  var httpClient = new HttpClient();
  
  httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
  var responseMessage = httpClient.PostAsync("&lt;URL&gt;", httpContent);
  var result = responseMessage.Result;
  </code></pre>
  
  <p>Once again, where <code>&lt;JSON&gt;</code> and <code>&lt;URL&gt;</code> are the valid JSON message and the endpoints's URL. It would seem  to me that the third line, on which I reference <code>httpCllient.DefaultRequestHeaders</code>, is not adding the <code>Accept: application/json</code> header to my request. If I manually add the header to the <code>httpContent.Headers</code> collection, I get a run-time error that tells me that <code>Accept</code> is not a header I can add to the <code>httpContent</code>. That's why I am hoping to add it to the <code>httpClient</code> instead. </p>
  
  <p>I have validated the URL and my JSON with Swagger, so I know those are correct. Also, the request is done over HTTPS, so I can't use Fiddler to validate that the <code>Accept</code> header is being included. And while I could enable decryption in Fiddler, that's a whole other ball of wax. I don't want to add their root certificate to my system, especially if I'm missing something fairly simple, which this seems to be. </p>
  
  <p>Any pointers will be appreciated. Thanks.</p>