title: HttpClient 400 bad request on PostAsync to endpoint after switching from Minimal API to MVC API
body: <p>I started out with a .NET 6 minimal api which was working fine, however I needed some extra functionality, so I then decided to switch over to MVC style api. Since then, when my WPF app tries to call the endpoint using POST I get a 400 bad request.
  If I use swagger to make the call, it works with no problem. But using HttpClient in my WPF app gives the error. I've looked at about a dozen other questions on this error but none of the solutions seemed to fix my problem.</p>
  <p>When I run both the API and WPF app with debugging and debug the beginning of both the UserService call to InsertUser and the API's InsertUser methods, the API's debug point never gets hit, which to me sounds like when MVC is processing the request it sees something it doesn't like and returns the 400 error, but I'm not sure why the PUT request is fine and the POST request isn't.</p>
  <p>In my ASP.net core program.cs I added the following to enable MVC api usage</p>
  <pre><code>builder.Services.AddControllers();
  app.MapControllers();
  </code></pre>
  <p>In my WPF app the httpClient is handled through DI and injected in my UserService class used to make the call to the endpoint.</p>
  <p>InsertUser POST method</p>
  <pre><code>public async Task&lt;bool&gt; InsertUser(UserModel user)
  {
      var result = await _httpClient.PostAsync(&quot;/api/Users&quot;,
          user.AsJsonContent());
      return result.IsSuccessStatusCode;
  }
  </code></pre>
  <p>This is the UpdateUser method that uses PUT and works fine</p>
  <pre><code>public async Task&lt;bool&gt; UpdateUser(UserModel user)
  {
      var result = await _httpClient.PutAsync(&quot;/api/Users&quot;, 
          user.AsJsonContent());
      return result.IsSuccessStatusCode;
  }
  </code></pre>
  <p>Extension that turns the UserModel into a StringContent</p>
  <pre><code>public static StringContent AsJsonContent(this object obj)
  {
      string json = JsonConvert.SerializeObject(obj);
      var content = new StringContent(json, Encoding.UTF8, &quot;application/json&quot;);
      //content.Headers.Remove(&quot;Content-Type&quot;);
      //content.Headers.Add(&quot;Content-Type&quot;, &quot;application/json&quot;);
      //content.Headers.ContentType = new MediaTypeHeaderValue(&quot;application/json&quot;);
      return content;
  }
  </code></pre>
  <p>This is a trimmed down version of my UsersController</p>
  <pre><code>[Route(&quot;api/[controller]&quot;)]
  [ApiController]
  public class UsersController : ControllerBase
  {
      [HttpPost]
      public async Task&lt;IActionResult&gt; InsertUser(UserModel model)
      {
          _appDbContext.Users.Add(model);
          var result = await _appDbContext.SaveChangesAsync();
          return result &gt; 0 ? Ok() : Problem(&quot;User was not inserted&quot;);
      }
  
      [HttpPut]
      public async Task&lt;IActionResult&gt; UpdateUser(UserModel model)
      {
          if(await _appDbContext.Users.FindAsync(model.Id) is UserModel efModel)
          {
              _mapper.Map(model, efModel);
              _appDbContext.Users.Update(efModel);
              var result = await _appDbContext.SaveChangesAsync();
              return result &gt; 0 ? Ok() : Problem(&quot;User was not updated&quot;);
          }
  
          return Problem(&quot;User not found.&quot;);
      }
  }
  </code></pre>
  <p>This is result from PostAsync</p>
  <pre><code>{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
  {
    Date: Thu, 07 Jul 2022 14:35:53 GMT
    Server: Kestrel
    Transfer-Encoding: chunked
    Content-Type: application/problem+json; charset=utf-8
  }}
  </code></pre>
  <p>This is my response message</p>
  <pre><code>{Method: POST, RequestUri: 'https://localhost:7159/api/Users', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
  {
    Content-Type: application/json
    Content-Length: 253
  }}
  </code></pre>
  <p>Edit: Adding reqested info</p>
  <p>Here is the json responce for the bad responce</p>
  <pre><code>{
      &quot;type&quot;: &quot;https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;,
      &quot;title&quot;: &quot;One or more validation errors occurred.&quot;,
      &quot;status&quot;: 400,
      &quot;traceId&quot;: &quot;00-ac9fc01b742547b2525eef9de28ea6a0-d123c5b603ff637c-00&quot;,
      &quot;errors&quot;: {
          &quot;Token&quot;: [
              &quot;The Token field is required.&quot;
          ]
      }
  }
  </code></pre>
  <p>This was the minimal api version of InsertUser</p>
  <pre><code>//Registered in another class
  app.MapPost(&quot;/Users&quot;, InsertUser);
  
  public async Task&lt;bool&gt; InsertUser(UserModel model)
  {
      await _appDbContext.Users.AddAsync(model);
      var result = await _appDbContext.SaveChangesAsync();
      return result &gt; 0;
  }
  </code></pre>