title: how to post form data using HttpClient into webapi in asp.net core
body: <p><a href="https://i.stack.imgur.com/Eok1D.png" rel="nofollow noreferrer">ScreenShot</a></p>
  
  <p>i am getting this error in c# code StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'.
   but in postman i am getting a status code 200 OK.</p>
  
  <pre><code>    public static void RunPostAsync(FindStaffModel inputs, string token)
      {
          HttpClient client = new HttpClient();
          ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
          ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) =&gt; true;
          client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
          client.BaseAddress = new Uri("https://api..................Enquiry");
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
          string json = JsonConvert.SerializeObject(inputs, Formatting.Indented);
  
          var buffer = System.Text.Encoding.UTF8.GetBytes(json);
  
          var byteContent = new ByteArrayContent(buffer);
  
          var httpResponce = new HttpResponseMessage();
          try
          {
              httpResponce = client.PostAsync("https://api..................Enquiry", byteContent).Result;
              using (HttpContent contents = httpResponce.Content)
              {
                  var result = contents.ReadAsByteArrayAsync().Result;
                  var res = (System.Text.Encoding.UTF8.GetString(result));
                  Console.WriteLine("result :" + res);
  
              }
          }
          catch (Exception ex)
          {
  
              Console.WriteLine("error" + ex);
              //Console.WriteLine("respose" + httpResponce);
          }
      }
  </code></pre>
  
  <blockquote>
    <p>i also try this code but i am getting same error
    StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
    {
      Cache-Control: no-cache
      Pragma: no-cache
      Server: Microsoft-IIS/10.0
      X-AspNet-Version: 4.0.30319
      X-Powered-By: ASP.NET
      Date: Mon, 06 Apr 2020 16:42:33 GMT
      Expires: -1
      Content-Length: 0
    }}</p>
  </blockquote>
  
  <pre><code>public static void RunPostAsync(FindStaffModel inputs, string token)
          {
  
              using (var handler = new HttpClientHandler() { CookieContainer = new CookieContainer() })
              {
                  using (var client = new HttpClient(handler) { BaseAddress = new Uri("https://api..................Enquiry") })
                  {
                      //add parameters on request
                      var body = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
                      {
                          new KeyValuePair&lt;string, string&gt;("FirstName", "test"),
                          new KeyValuePair&lt;string, string&gt;("LastName", "test"),
                          new KeyValuePair&lt;string, string&gt;("JobTitleId", "1"),
                          new KeyValuePair&lt;string, string&gt;("JobTitle", ".Net Developer"),
                          new KeyValuePair&lt;string, string&gt;("JobTypeId", "2"),
                          new KeyValuePair&lt;string, string&gt;("JobType", "Part Time") . . . . . . . .
  
  
                      };
  
                      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api...............Enquiry");
  
                      //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded; charset=UTF-8"));
                      client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
                      client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
                      client.DefaultRequestHeaders.Add("X-MicrosoftAjax", "Delta=true");
                      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  
                      client.Timeout = TimeSpan.FromMilliseconds(10000);
  
                      var res = client.PostAsync("https://api.........Enquiry", new FormUrlEncodedContent(body)).Result;
  
                  }
              }
          }
  </code></pre>