title: ASP .Net Core: Post API with Cors with request body giving 400 bad request
body: <p>This is a quite repeated question on SO and almost 7-8 questions already answered but the existing solutions aren't working for me.</p>
  <p><strong>When I run the server with same code over localhost, it works fine. But it doesn't work after deployment and all my POST request with body give 400 bad request errors.</strong></p>
  <p><strong>Frontend</strong>: Angular 7.x running on <code>http://localhost:4200</code></p>
  <p><strong>Server</strong>: Asp .net Core 3.1 running on Ubuntu machine on AWS via <code>Nginx</code> over <code>Cloudflare</code> domain server.</p>
  <p><strong>Startup.cs</strong> : Here I'm using the default CORS middleware of ASP. I've also tried writing a custom middleware and a extension method for entertaining CORS requests. I'll also add it if you need that code flow. That also didn't work.</p>
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      public void ConfigureServices(IServiceCollection services)
      {
          IdentityModelEventSource.ShowPII = true;
  
          services.AddLogging();
  
          services.AddCors();
          services.AddControllers().AddNewtonsoftJson();
          services.AddHealthChecks();
  
          services.AddDistributedMemoryCache();
          services.AddMemoryCache();
  
          services.AddEntityFrameworkNpgsql().AddDbContext&lt;EcommerceDbContext&gt;();
  
          services.AddHttpClient();
          services.AddSession();
  
          services.AddScoped&lt;EcommerceDbContext&gt;();
  
          services.AddSwaggerDocument();
  
          services.Configure&lt;ApiBehaviorOptions&gt;(options =&gt;
          {
              options.SuppressModelStateInvalidFilter = true;
          });
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseRequestResponseLogging();
  
          app.UseOpenApi();
          app.UseSwaggerUi3();
  
          app.UseSession();
  
          app.UseRouting();
          app.UseCors(builder =&gt; {
             builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
          });
  
          app.UseEndpoints(endpoints =&gt;
          {
              endpoints.MapControllers();
              endpoints.MapHealthChecks(&quot;/health&quot;);
          });
      }
  }
  </code></pre>
  <p><strong>PlaceOrderController.cs</strong> : The <code>/placeOrder</code> API is not working. Also tried with and without <code>FromBody</code> attribute. The log is not getting printed but in <code>nginx access.log</code> it says 400 status code.</p>
  <pre><code>public class PlaceOrderController : Controller
  {
      private readonly ILogger logger;
  
      public PlaceOrderController(ILoggerFactory loggerFactory)
      {
          logger = loggerFactory.CreateLogger&lt;PlaceOrderController&gt;();
      }
  
      [HttpPost]
      [Route(&quot;/placeOrder&quot;)]
      public async Task&lt;ActionResult&gt; PlaceOrder(OrderRequest body)
      {
          // this log is not getting printed
          logger.LogInformation(&quot;Hello world&quot; + body.ToString());
  
          // rest of the code removed
      }
  }
  </code></pre>
  <p><strong>OrderRequest.cs</strong> : This is the model for the POST request body. Since my model contains a field with underscore, I've tried with both the native serializer <code>System.Text.Serialization</code> and <code>Newtonsoft.Json</code> as well. That also didn't work.</p>
  <pre><code>public class OrderRequest
  {
      public OrderRequest()
      {
      }
  
      [JsonProperty(&quot;rate&quot;)]
      public double Rate;
  
      [JsonProperty(&quot;quantity&quot;)]
      public double Quantity;
  
      [JsonProperty(&quot;cashback_rate&quot;)]
      public string CashbackRate;
  
      public override string ToString()
      {
          return JsonConvert.SerializeObject(this);
      }
  }
  </code></pre>
  <p><strong>Sample Request Body</strong> : <code>{&quot;rate&quot;:12.0,&quot;quantity&quot;:1.0,&quot;cashback_rate&quot;:&quot;enterprise&quot;}</code></p>
  <p>Please help. Close to 4 working days already wasted. :(</p>
  <p>Thanks in advance.</p>