title: Building post HttpClient request in C# with Bearer Token
body: <p>I'm not really a C# expert and I have a post httpRequest in C# to develop and for this I created this method that takes a Uri, an object and a bearer token.</p>
  <p>This method aims to build the calling request:</p>
  <pre><code>private HttpClient client = new HttpClient(); 
  public async Task&lt;UserResponse&gt; CreateUser(Uri url, UserRequest userRequest, string token)
  {
          
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
      client.DefaultRequestHeaders.TryAddWithoutValidation(&quot;Content-Type&quot;, &quot;application/json; charset=utf-8&quot;);
  
  
      string requestObject = JsonConvert.SerializeObject(userRequest);
      Console.WriteLine(&quot;My Object: &quot; + requestObject);
      var req = new HttpRequestMessage(HttpMethod.Post, url);
      req.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, token);
      req.Content = new StringContent(
          requestObject,
          Encoding.UTF8,
          &quot;application/json&quot;
      );
  
      Console.WriteLine(req.ToString());
      var response = await client.SendAsync(req); 
          
      string output = await response.Content.ReadAsStringAsync();
      Console.WriteLine(JsonConvert.DeserializeObject(output));
  
      UserResponse returnValue = JsonConvert.DeserializeObject&lt;UserResponse&gt;(output);
      return returnValue;
  }
  </code></pre>
  <p>My issue is that i'm not sure I'm passing correctly my header content. The return response is an error message telling I'm not authenticated.</p>
  <p>Thanks</p>