I am creating an ASP.net MVC website with a RESTful API to a SQL database. I have implemented a controller which holds the HTTP commands.
One of the commands is a POST
command:
// POST: api/PoolTests
[ResponseType(typeof(PoolTest))]
public IHttpActionResult PostPoolTest(PoolTest poolTest)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.PoolTests.Add(poolTest);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = poolTest.Id }, poolTest);
}
I am using Fiddler to test out the function and I am getting some strange results. According to the automatically generated API documents the request format should be JSON with this structure (These are the fields to the connect SQL database):
{
"Id": 1,
"SiteID": "sample string 2",
"Date": "sample string 3",
"Tub": "sample string 4",
"par1": 5.1,
"par2": 6.1,
"par3": 7.1,
"par4": 8.1
}
In the fiddler composer I select POST
and http://localhost:53660/api/PoolTests
and the JSON payload {"Id": 1,"SiteID": "sample string 2", "Date": "sample string 3","Tub": "sample string 4","par1": 5.1, "par2": 6.1,"par3": 7.1,"par4": 8.1}
This results in a HTTP 400 error code.
If I send the same request with no JSON (payload) then it breaks at the line db.PoolTests.Add(poolTest);
because the payload is null.
With payload if I place a break point at line if (!ModelState.IsValid)
the breakpoint is never reached. It is almost like with payload the actual HTTP command is not being recognised.
Any thoughts - I appreciate that there may be some details missing.
ADDED: PoolTest Class
public class PoolTest
{
public int Id { get; set; }
public string SiteID { get; set; }
public string Date { get; set; }
public string Tub { get; set; }
public double par1 { get; set; }
public double par2 { get; set; }
public double par3 { get; set; }
public double par4 { get; set; }
}
FIDDLER Screenshot Added:
I have added the content-type
and now I hit the breakpoint, but the Pooltest
is reported as being null