Ron Ron - 3 months ago 11
C# Question

Testing WebApi with Fiddler POST

GET works fine. In the Controller I have this method:

[HttpPost]
public IHttpActionResult Post(Product product)
{
var isSaved = productService.SaveProduct(product);
if (isSaved == true) return Ok();
else return BadRequest();
}


The problem is, product is null. I get to this method (via a breakpoint) but am not sure why the product is null. Here's what's in Fiddlers Composer:

User-Agent: Fiddler
host: localhost:53882
Content-Type: application/json; charset=utf-8
Content-Length: 54


And this is the Fiddler request body:

{
id:5,
name: Dress,
price: 39.90,
quantity:71
}


enter image description here
There's no model. It's all in the db (edmx). It works for getting a list of products, or a product by product id.

Answer

The name in your request body is missing the double quotes. Make sure the cases matches as your model because it is case-sensitive.

{
  id:5,
  Name: "Dress",
  Price: 39.90,
  Quantity:71
}