Search code examples
postasp.net-web-apifiddlerasp.net-web-api-routing

Model is null while POSTing to web api using fiddler (test)


Hi I have the below Model class for my project. I am trying to get Form values in my WEB API POST method in Values Controller, however the values received are null. I am here for your suggestion on getting the values as was posted in Form

 public class DetailModel
{
    public string FirstName { get; set; }
    public string MiddleName { get; set;}
    public string LastName { get; set; }
    public string Phone { get; set;}
    public string Email { get; set; }
}

The below shown is the api controller code

 public void Post([FromUri] DetailModel model)
    {

            try
            {
                SMTravelsEntities st = new SMTravelsEntities();
                st.spTravelAPIDataCreation(model.FirstName, model.MiddleName, model.LastName, model.Phone, model.Email);
                st.SaveChanges();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }            

    }

I have tried both the [FromBody] and [FromUri] for receiving the model data. However I am getting the null value in my "DetailModel".

Here is my code for Routes

 config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/",
            defaults: new { id = RouteParameter.Optional }
        );

I have used fiddler to test it

{

"FirstName": "Sarath", "MiddleName":"MM" "LastName": "FF", "Phone": "0000", "Email":"s@s.com" }

User-Agent: Fiddler

Host: localhost:1194 Content-Length: 103 Content-Type: application/json


Solution

  • This is what I did to address the issue.

     public DetailModel Post([FromBody] DetailModel model)
        {
                try
                {
                    SMTravelsEntities st = new SMTravelsEntities();
                    st.spTravelAPIDataCreation(model.candidateFirstName, model.candidateMiddleName, model.candidateLastName, model.candidatePhoneNumber, model.candEmail);
                    st.SaveChanges();
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
                return model;
        }