Q:

Client.PostAsync (with Json) Gets 400 Bad Request

I am trying to create an Integration Test for my .Net Core Web Api
But I am always getting a 400 Bad Request response. I am sharing details below
Here is my Controller method
public IActionResult UpdateProductById([FromBody]int id, string description)
{
    var result = ProductService.UpdateProductById(id, description);
    if (result.Exception == null)
        return Ok(result);
    else
        return BadRequest(result.Exception.Message);
}

Here is my test class (which tries to post)
[Fact]
public async Task UpdateProductById_Test_WithProduct()
{
    var product = new
    {
        id = 1,
        description = "foo"
    };

    var productObj= JsonConvert.SerializeObject(product);

    var buffer = System.Text.Encoding.UTF8.GetBytes(productObj);
    var byteContent = new ByteArrayContent(buffer);

    byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    var result = await _tester.Client.PostAsync("/api/1.0/UpdateProductById", byteContent);

    result.StatusCode.Should().Be(HttpStatusCode.OK);
}

A:

The test is sending all the content in the body of the request, yet the action is only binding the id. Most likely the description is null and causing an issue with the update.
Create a model to hold the data
public class ProductModel {
    public int id { get; set; } 
    public string description { get; set; }
}

Refactor action to get the content from the body of the request
[HttpPost]
public IActionResult UpdateProductById([FromBody]ProductModel model) {
    if(ModelState.IsValid) {
        var result = ProductService.UpdateProductById(model.id, model.description);
        if (result.Exception == null)
            return Ok(result);
        else
            return BadRequest(result.Exception.Message);
    }
    return BadRequest(ModelState);
}

