1

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 POSTcommand:

     // 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

enter image description here

8
  • Could you post your PoolTest class? Does it have validation rules on it to enforce? Commented Jun 16, 2015 at 16:04
  • @br4d - added the PoolTest class Commented Jun 16, 2015 at 16:09
  • Your request must have header "Content-Type:application/x-www-form-urlencoded". Please check if you have this header or not. Commented Jun 16, 2015 at 16:28
  • @alisabzevari - I have added the content-type as you have suggested. The breakpoint is now hit, however, pooltest is reported as being null. I have added a screenshot of my fiddler request which may contain errors around data. Commented Jun 16, 2015 at 16:41
  • Now I think [FromBody] will work! Commented Jun 16, 2015 at 16:43

2 Answers 2

1

You need to move the data to the Request Body. In Fiddler it would be a separate input box (below the one in your screenshot).

Your Headers for POST http://localhost:53660/api/pooltests become:

User-Agent: Fiddler
Host: localhost:53660
Content-Type: application/json
Content-Length: 140

The headers Content-Type should be application/json and the Content-Length will be computed for you. You are defining a header data which isn't correct.

In the Request Body you can paste your data

{"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}

You can leave your action signature alone but [FromBody] is helpful when you want to add parameters to the URL.

[ResponseType(typeof(PoolTest))]
public IHttpActionResult PostPoolTest([FromBody] PoolTest poolTest)
{
    ...
}

An AJAX request with jQuery would look like this:

$.ajax({
    url: "http://localhost:53660/api/pooltests",
    method: "POST",
    data: { "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 }
})
.done(function(result) {
    console.log("success", result);
})
.fail(function(xhr, txtStatus) {
    console.log("error", txtStatus);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The problem was that the Request Body was not displaying the way I had fiddler docked. Secondly, I was neglecting the content-length. Both these resulted in the Request Body data being received. Although I now I have a slight dilemma - another question!
1

Try changing your method signature to:

public IHttpActionResult PostPoolTest([FromBody]PoolTest poolTest)

This should allow you to at least hit your breakpoint. From there you can see model errors.

1 Comment

Did you get it figured out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.