title: C# HttpWebRequest vs HttpClient vs RestSharp
body: <p>I'm trying to send a simple post request in 3 different ways, first two are failing with 400 Bad Request, only RestSharp works. I would say the body of the request and the headers are set in the same way, so I don't understand what's wrong.</p>
  <h3>Using <code>HttpWebRequest</code></h3>
  <pre><code>var httpRequest = (HttpWebRequest) WebRequest.Create(uri);
  httpRequest.Method = &quot;POST&quot;;
  httpRequest.Accept = &quot;application/json&quot;;
  httpRequest.Headers[&quot;Authorization&quot;] = $&quot;Bearer {token}&quot;;
  httpRequest.Headers[&quot;Client_Id&quot;] = clientId;
  httpRequest.ContentType = &quot;application/json&quot;;
  
  using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
  {
      streamWriter.Write(json);
  }
  
  var httpResponse = (HttpWebResponse) httpRequest.GetResponse();
  </code></pre>
  <h3>Using <code>HttpClient</code></h3>
  <pre><code>using (var client = new HttpClient())
  {
      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, token);
      client.DefaultRequestHeaders.Add(&quot;Client_Id&quot;, clientId);
      client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue(&quot;gzip&quot;));
      client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue(&quot;deflate&quot;));
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;text/json&quot;));
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;text/x-json&quot;));
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;text/javascript&quot;));
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/xml&quot;));
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;text/xml&quot;));
  
      var content = new StringContent(json, Encoding.UTF8, &quot;application/json&quot;);
  
      var result = client.PostAsync(uri, content).Result;
  }
  </code></pre>
  <h3>raw in Fiddler</h3>
  <pre><code>POST https://uri HTTP/1.1
  Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
  Content-Type: application/json
  Client_Id: 123456
  Authorization: Bearer token
  Accept-Encoding: gzip, deflate
  Host: uri
  Content-Length: 314
  Expect: 100-continue
  Connection: Keep-Alive
  </code></pre>
  <h3>Using <code>RestClient</code></h3>
  <pre><code>var restClient = new RestClient(uri);
  var request = new RestRequest(Method.POST);
  request.AddHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
  request.AddHeader(&quot;Client_Id&quot;, clientId);
  request.AddHeader(&quot;Authorization&quot;, $&quot;Bearer {token}&quot;);
  request.AddParameter(&quot;application/json&quot;, json, ParameterType.RequestBody);
  IRestResponse response = restClient.Execute(request);
  </code></pre>
  <h3>raw in Fiddler</h3>
  <pre><code>POST https://uri HTTP/1.1
  Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
  Content-Type: application/json
  Client_Id: 123456
  Authorization: Bearer token
  User-Agent: RestSharp/106.12.0.0
  Host: uri
  Content-Length: 314
  Accept-Encoding: gzip, deflate
  Connection: Keep-Alive
  </code></pre>