title: How to call POST method from API using HttpClient?
body: <p>I am trying to use the HttpClient class to call a POST method from my API which is supposed to add Server information to a DB. </p>
  
  <p>The way I'm trying to call it is below. But when I step through it to debug it steps through my if statement for response.IsSuccessStatusCode. </p>
  
  <pre><code>public static async Task&lt;Server&gt; PostServer(Server server)
      {
          using (var client = new HttpClient())
          {
              client.BaseAddress = new Uri("http://localhost:50489/");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              StringContent content = new StringContent(JsonConvert.SerializeObject(server));
              // HTTP POST
              HttpResponseMessage response = await client.PostAsync("api/Server/", content);
              if (response.IsSuccessStatusCode)
              {
                  string data = await response.Content.ReadAsStringAsync();
                  server = JsonConvert.DeserializeObject&lt;Server&gt;(data);
              }
          }
          return server;
      }
  </code></pre>
  
  <p>Also here is the POST method in my API below it was automatically generated in VS.</p>
  
  <pre><code>// POST: api/Servers
      [ResponseType(typeof(Server))]
      public async Task&lt;IHttpActionResult&gt; PostServer(Server server)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          db.Servers.Add(server);
          await db.SaveChangesAsync();
  
          return CreatedAtRoute("DefaultApi", new { id = server.Server_ID }, server);
      }
  </code></pre>