title: C# - Set HttpClient Headers to POST data to Azure REST API
body: <p>I am trying to post some data to an Azure REST API. I have a request defined in Postman that works. Now, in my C# code, I want to use the <code>HttpClient</code> instead of the helper libraries. In an attempt to do this, I currently have:</p>
  
  <pre><code>try
  {
    var json = @"{
      '@search.action':'upload',
      'id':'abcdef',
      'text':'this is a long blob of text'
    }";
  
    using (var client = new HttpClient())
    {
      var requestUri = $"https://my-search-service.search.windows.net/indexes/my-index/docs/index?api-version=2019-05-06";
  
      // Here is my problem
      client.DefaultRequestHeaders.Clear();
      client.DefaultRequestHeaders.Add("api-key", myKey);
      client.DefaultRequestHeaders.Add("Content-Type", "application/json");
  
      using (var content = new StringContent(json, Encoding.UTF8, "application/json")
      {
        using (var request = Task.Run(async() =&gt; await client.PostAsync(requestUri, content)))
        {
          request.Wait();
          using (var response = request.Result)
          {
          }
        }
      }
    }
  }
  catch (Exception exc)
  {
    Console.WriteLine(exc.Message);
  }
  </code></pre>
  
  <p>When I run this, an <code>InvalidOperationException</code> gets thrown that says:</p>
  
  <pre><code>Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
  </code></pre>
  
  <p>I don't understand what I've done wrong. How do I post data to an Azure REST API using the <code>HttpClient</code> in C#?</p>
  
  <p>Thank you!  </p>